[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

question about File I/O

Posted on 2002-03-28
2
Medium Priority
?
235 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:
Vinayak Kumbar earned 141 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Integration Management Part 2
Suggested Courses

834 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