Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Reading .TXT Files in MFC

Posted on 2010-09-21
5
Medium Priority
?
1,197 Views
Last Modified: 2013-11-20
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
input.Close();

Open in new window

0
Comment
Question by:mr_stevie
5 Comments
 
LVL 33

Assisted Solution

by:pgnatyuk
pgnatyuk earned 400 total points
ID: 33731842
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.
0
 
LVL 29

Accepted Solution

by:
pepr earned 1600 total points
ID: 33731877
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."
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 33731890
Showing the complete code would help - for instance what is P.continue, how is it changed in value?

Also note:
CStdioFile::ReadString
Reading is stopped by the first newline character.
virtual BOOL ReadString(    CString& rString );
0
 
LVL 5

Expert Comment

by:greatsubash
ID: 33732183
0
 
LVL 1

Author Closing Comment

by:mr_stevie
ID: 33750631
Works like a charm. Thanks.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seveā€¦
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question