Solved

Reading a file...

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
move a line in eclipse 3 103
Unix Command -- Challenging  question 7 90
Trying to run powershell  in a batch file. How do I do this? 8 92
Problem to App 4 83
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…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

777 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