Click to See Complete Forum and Search --> : GetFileSize


eeboy
January 18th, 2006, 06:06 PM
I am attempting to read a text file (entire file) into a buffer using ReadFile. I specify the number of bytes with a call to GetFileSize. Everything works fine except the last 10 or 15 bytes of the file are left off. I've debugged a bit and I find that GetFileSize returns the number of 'viewable' characters. The clipped portion is exactly equal to the number of carriage returns in the text file. Can anyone explain why this is... and/or offer a beter solution for GetFileSize?

Thanks!

kirants
January 18th, 2006, 06:32 PM
There must be something else going on.
Please post all relavant porstions of the code used to open file, get file size and read file.

eeboy
January 18th, 2006, 08:26 PM
Ok, eliminating error checking on return values my code boils down to:

hFile=CreateFile(filePath,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORM,
NULL);

lFileSize=GetFileSize(hFile,NULL); //Small files... no need to check upper 32

sBuf=new char[lFileSize];

ReadFile(hFile,sBuf,lFileSize,&lpBytesRead,NULL)

At this point I can examine the string in the debugger and it's clipped by exactly the number of CR's in the text file. Any suggestions?

Also, I am certain that there are no null values in the text file.

Thanks!

Calculator
January 18th, 2006, 08:48 PM
I do not encounter this. Can you post the file you are using?

eeboy
January 18th, 2006, 09:42 PM
This is my test file:

Calculator
January 19th, 2006, 09:53 PM
And then you did something along the lines of?:

szBuf[dwBytesRead] = 0;
printf("%s", szBuf);

golanshahar
January 20th, 2006, 01:44 PM
This is my test file:

I tested your code with the file you posted and it seems ok, I showing in the message box the whole buffer that I read from the file and it looks ok.


HANDLE hFile =::CreateFile("c:\\test.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if ( hFile == INVALID_HANDLE_VALUE)
return;
DWORD dwFileSize =::GetFileSize(hFile,NULL);
if ( dwFileSize <= 0 )
return;

char *szBuf=new char[dwFileSize];
DWORD dwBytes=0;
::ReadFile(hFile,szBuf,dwFileSize,&dwBytes,NULL);
::MessageBox(0,szBuf,"Test",MB_OK);
delete []szBuf;
::CloseHandle(hFile);



so i dont know what is the problem :confused:

Cheers