Simple file I/O is giving me trouble
-
I am trying to read the entire content of a text file with the following code:
File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close();
The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned? -
I am trying to read the entire content of a text file with the following code:
File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close();
The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned?Hi, Think having some prob with EOF state. Have a look @ msdn for basic_istream::seekg. This may help you Sujan
-
I am trying to read the entire content of a text file with the following code:
File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close();
The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned?Hi For reading entire file content, try use block (not stream) i/o.
FILE *pFile = fopen(fname, fmode); // fmode = "r" for text files and "rb" for binary data fseek(pFile, 0, SEEK_END); long len = ftell(pFile); char *data = new char[len]; fseek(pFile, 0, SEEK_SET); long rest = len; long tpos = 0; while(rest) { long rd = fread(data+tpos, 1, rest, pFile); if (!rd) { // check error } else { rest -= rd; tpos += rd; } fclose(pFile)
-
I am trying to read the entire content of a text file with the following code:
File.seekg(0, ios::end); long lFileSize = File.tellg(); szFileBuffer = new char[lFileSize + 1]; if ( szFileBuffer == NULL ) { MessageBox(NULL, "There is not enough memory to run this application", NULL, MB_ICONERROR | MB_OK); return 1; } File.seekg(0, ios::beg); File.get(szFileBuffer, lFileSize + 1, EOF); File.close();
The actual filesize is 715KB but the program is only reading 540KB...tellg gives the correct filesize yet get seems to only be getting 540KB...are there any workarounds for this...in general, are there buffer size limitations as far as istream::get is concerned?How are you opening the file? If the file is opened as "text" then any EOF character encountered before the last one will cause the
get()
call to end prematurely.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
-
How are you opening the file? If the file is opened as "text" then any EOF character encountered before the last one will cause the
get()
call to end prematurely.
"Opinions are neither right nor wrong. I cannot change your opinion of me. I can, however, change what influences your opinion." - David Crow
Thanks for the replies...yes the file is being opened as text...there shouldn't be a premature EOF character in there...but in general, what are my options while keeping this file open as text?