Solved

Reading .TXT Files in MFC

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 218
Add values of each row in an array 3 58
How to measure sizes and angles in scanned images ? 3 63
FMX TCameraComponent Problem 2 70
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…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

810 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