Solved

Reading a file...

Posted on 2004-04-05
5
165 Views
Last Modified: 2013-11-20
Using MFC VC++6, I created a file which has the character 'a' at position 1.  I want to read the file and convert it  to a CString  and then display it to a message box.

Here is the code I have been working with.

const int MAX_STRING_LENGTH = 10;
CString MyString;
CFile MyFile;
MyFile.Open("...MyfilePath", CFile::modeNoTruncate | CFile::modeRead   );

char* TempBuf = MyString.GetBuffer(MAX_STRING_LENGTH);
UINT NumChars = MyFile.Read((void FAR*) TempBuf, MAX_STRING_LENGTH - 1);
 MyString.ReleaseBuffer(NumChars);

 AfxMessageBox(MyString);

I get a run time exception error when I run this code.

Is there an easier way to do this?

Thanks,
RABMissouri
0
Comment
Question by:rabmissouri
[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
  • 3
5 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 100 total points
ID: 10756677
Add 0 to the end of string:

UINT NumChars = MyFile.Read((void FAR*) TempBuf, MAX_STRING_LENGTH - 1);
TempBuf[NumChars] = 0;
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10756686
Another way is using CStdioFile Class which has ReadString member:

BOOL ReadString(CString& rString);
0
 

Author Comment

by:rabmissouri
ID: 10762285
It works.  Thanks.  I think my main difficulty was converting a char array to a CString.  Is there an easy way to do this?  Why did you have me add the line "TempBuff[NumChars]=0"

Thanks again,
RABMissouri
0
 

Expert Comment

by:palani_vs
ID: 10764669
Hi
if u want to just assign a char array to cstring, u can use Format function.

vs
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 10766133
Why did you have me add the line "TempBuff[NumChars]=0"

Because 0 means end of string. Without 0 in the end any string function may fail continuing to read memory block.
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: 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: 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.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

696 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