Solved

question about File I/O

Posted on 2002-03-28
2
226 Views
Last Modified: 2013-11-20
In my programing,when I use CFile::Read(string,file->GetLength())to read the file and display the string
in a Edit Box
however,sometimes at the end of the content ,there comes
some unexpected characters,sometimes there is no this kind of char,
could you tell me why and what shoul I do?

thanx
0
Comment
Question by:vincent_lea
2 Comments
 
LVL 8

Accepted Solution

by:
VinExpert earned 47 total points
ID: 6904601
Hi,

I guess, there is no null termination to the string, so it is happening. The next guess is that, that caracter may be something equivalent to '\n' or some special character.

Try doing something like this

char *pBuff = NULL;
CFile cFile;
if(cFile.Open("Mine.txt", CFile::modeRead))
{
int nFileLen = cFile.GetLength();
pBuff = new char[nFileLen + 1];
memset(pBuff, 0, nFileLen + 1);
UINT nBytesRead = cfile.Read(pBuff, nFileLen);
pBuff[nFileLen] = 0;//Null termination
//Update ur edit control
if(pBuff)
{
delete pBuff;
pBuff = NULL;
}
}

Try it out.
VinExpert
0
 
LVL 3

Expert Comment

by:cypherljk
ID: 6909592
In addition to what VinExpert said a much easier way to read strings (just text data) is to use the CStdioFile class which is derived from CFile. This class is very easy to use and null terminates the strings. Also you read/write into a CString object and don't have to worry about allocating memory. ;-)

Try it.. Here a link to the documentation (being derived from CFile it's almost the same except that there is less work to do.  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_MFC_CStdioFile.asp

My 2 cents...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
cat dog challenge 18 122
iSeries DB2 Query 2 84
Best tools for Backup and restore of Wordpress site 6 53
matchUp  challenge 6 48
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

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