filesize error

I

Imran

Hello All

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;

}
bool GetData( char* &pData)
{



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


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


//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

THanks in Adv
 
V

Victor Bazarov

Imran said:
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
 
G

Gernot Frisch

//read the data back and update the passes variable
if(fp)
fread(pData, sizeof( char ),pos,fp);
am using MSVC as the compiler

try: fread(pData, pos, 1, fp);

I think I got some problem with this as well, but I can't remember
anymore...

If not, let us see the writing code...
-Gernot
 
H

Howard

Imran said:
Hello All

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;

delete [] pDate;
// you allocate an array, so you must delete an array

I'm not that familiar with the file functions, so I won't analyse the read
function code. But, you've only *given* a read function! You say that you
have a problem writing back to a file, but you don't show any write
function. Maybe the problem's there?

-Howard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,241
Latest member
Lisa1997

Latest Threads

Top