Solved

Reading a file...

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Question regarding Copy/Paste 16 87
Process filename extension 3 157
sum67 challenge 35 89
lucky13 challenge 11 112
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

747 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now