Solved

Reading .TXT Files in MFC

Posted on 2010-09-21
5
1,097 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 100 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 28

Accepted Solution

by:
pepr earned 400 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 44

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

911 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now