Solved

Reading a file...

Posted on 2004-04-05
5
162 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

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
Unix / Linux grid computing 5 169
How to determine file rename from EventLog ? 2 58
centeredAverage challenge 8 186
Adjust the codes 3 35
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…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

726 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