Re: filesize error

From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 08/10/04


Date: Tue, 10 Aug 2004 10:24:24 -0400

Imran wrote:
> Am trying to read a huge txt file and write to a file back.
>
> pls see the code
>
> int main()
> {
> char* pData;
>
> //write to some file
> GetData( pData);
>
> delete pData;

You mean

      delete[] pData;

, don't you?

>
> }
> bool GetData( char* &pData)
> {
>
>
>
> char pTempFileLocation="C:\\temp\\test.txt";

That shouldn't compile. Did you mean to write

   const char* const pTempFileLocation = "C:\\temp\\test.txt";

?

>
>
> //read back the data from the temp file
> FILE *fp = fopen(pTempFileLocation,"r+t");

Opening the file as text is what screws it up. Open it as binary.

>
>
> //This Code is to find the size of the file
> //simplest way
> fseek(fp,0L,SEEK_END);
>
> //now file poitner in the last poisition.
> //so pos will have the size :)
> long pos = ftell(fp);
>
> //move the file pointer back to begin
> //for reading..
> fseek(fp,0L,SEEK_SET);*/
>
> //allocate the data with the pos
> pData = new char[pos];
>
> //Just Inializing the pData to null
> //for safe side initialization
> memset(pData,0,pos);
>
> //read the data back and update the passes variable
> if(fp)
> fread(pData, sizeof( char ),pos,fp);
> else
> // file not found
> return false;
> fclose(fp);
>
> return true;
> }
>
>
> The problem is, I am getting the filesize correctly in the pos var. And am
> allocatiin the pData correctly.
> but while writing back to new file, it writes some more junk characters.Y it
> is so.
>
> File size is > 3MB.
>
> am using MSVC as the compiler

See my comment inline with your post.

HTH

V



Relevant Pages

  • Pass long* to VC++COM object from C#
    ... public void PutData(ref int pData, ... With the first function I pass a buffer to the component and the ...
    (microsoft.public.dotnet.framework.interop)
  • Re: Pocket PC - Pointer Crash
    ... and a DWORD must be DWORD aligned. ... PUSHORT pData; ... int FOO ...
    (microsoft.public.pocketpc.developer)
  • Re: Pass long* to VC++COM object from C#
    ... If you pass some kind of managed array like byte, ... declare void PutData(IntPtr pData, int nBytes); ... IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement ...
    (microsoft.public.dotnet.framework.interop)
  • Re: How can I tell if an [out] parameter is NULL?
    ... The way to marshal these is by writing a pair of routines that are ... compiled and linked in the proxy/stub DLL. ... HRESULT IF_GetData_Proxy(LPVOID pThis, BYTE* pData) ... compile. ...
    (microsoft.public.win32.programmer.ole)
  • Re: Pocket PC - Pointer Crash
    ... PUSHORT pData; ... I'm de-referencing a pointer who's contents/values I can view without any ... problems in the memory watch window. ... int FOO ...
    (microsoft.public.pocketpc.developer)