?
Solved

question about File I/O

Posted on 2002-03-28
2
Medium Priority
?
232 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 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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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