Solved

Reading .TXT Files in MFC

Posted on 2010-09-21
5
1,156 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 29

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

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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…
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…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

617 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