Solved

question about File I/O

Posted on 2002-03-28
2
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: 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

Suggested Solutions

Title # Comments Views Activity
while loop over for loop 7 119
countEvens challenge 2 116
Problem to App 4 145
Example code 13 42
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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

737 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