Solved

Reading .TXT Files in MFC

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Visual Studio 2015 locks debug executable 9 107
Doc'in system (example?) BA 7 79
canBalance challenge 34 65
Path of Workbook 3 45
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
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 viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 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

12 Experts available now in Live!

Get 1:1 Help Now