[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Reading a file...

Posted on 2004-04-05
5
Medium Priority
?
172 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 400 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

Industry Leaders: 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: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

656 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