Solved

question about File I/O

Posted on 2002-03-28
2
228 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

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

777 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