Reading .TXT Files in MFC

Hello EE,

I am simply trying to read a .TXT file into my program and store it as a vector of CStrings (see attached code).

However, there seem to be EoF characters contained in some of the .TXT files and cutting short my input reading.

Is there anyway to ensure that when reading from a file, that the entire file is read?

Thank you in advance.
// Variables
CStdioFile input;
vector<CString> readings;

// Open the file
input.Open( full_filename, CFile::modeRead );
// While there is something to read, store it in the readings vector
while( input.Read( buffer ) && P.Continue )
	readings.push_back( buffer );

// Close the input file

Open in new window

Who is Participating?
peprConnect With a Mentor Commented:
If EOF character may be expected, and it must not stop reading from the file, then you must open and read the file in binary mode.  Or you can prepare some fixing utility that removes the unwanted binary characters before the files are consumed by the application.

"When the complexity reached some degree, put an agent in between."
pgnatyukConnect With a Mentor Commented:
You can use CFile class. Detect the file size. Allocate the buffer big enough to load the file. Read the entire file to that buffer. Then, you can parse it as you wish. For example push to the vector string lines - there is carriage return/line feed in the end of each line. When the data is in the memory, you can modify it and remove characters you don't like.
AndyAinscowFreelance programmer / ConsultantCommented:
Showing the complete code would help - for instance what is P.continue, how is it changed in value?

Also note:
Reading is stopped by the first newline character.
virtual BOOL ReadString(    CString& rString );
mr_stevieAuthor Commented:
Works like a charm. Thanks.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.