Re: filesize error
From: Victor Bazarov (v.Abazarov_at_comAcast.net)
Date: 08/10/04
- Next message: Gernot Frisch: "Re: filesize error"
- Previous message: News For Mohan: "Re: Quick sort and memory problems"
- In reply to: Imran: "filesize error"
- Next in thread: Gernot Frisch: "Re: filesize error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Gernot Frisch: "Re: filesize error"
- Previous message: News For Mohan: "Re: Quick sort and memory problems"
- In reply to: Imran: "filesize error"
- Next in thread: Gernot Frisch: "Re: filesize error"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|