Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

convert char* to MFC CString

Posted on 2015-01-22
5
Medium Priority
?
265 Views
Last Modified: 2015-02-08
I have declared a char* strLine, then I allocated mem to be used to read a text file one line at a time as below:

char* strLine = (char*) malloc(sizeof(char) * 2048);
FILE *fp;
fp = fopen(myFilePath, "rt");

//code to read in a line one character at a time till it reach '\n'
//then strLine[lastIndex] = '\0'

//below is where problem happens
CString str(strLine);

Open in new window


the CString is a MFC CString and it always fail with the following message:
CMemoryException at memory location 0x0021ebe0

If I use a fixed length char array, such as char strLine[2048], then CString str(strLine) doesn't have any problem. So it seem to me, MFC CString constructor wants a const char array with a fixed length, but I also read somewhere that MFC CString constructor can also take a null-terminated char array (char *), so could someone help explain why I can't use the dynamically allocated char array to instantiate a MFC CString?

The program uses MFC in a shared dll, not using ATL, uses Multi-Byte Character Set.
0
Comment
Question by:TongZ
[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
  • 2
5 Comments
 
LVL 71

Expert Comment

by:Qlemo
ID: 40564629
CString can take a char*. But you might have used Unicode settings in your project, making all characters 16 bit wide. Either change that, or use TCHAR (which requires to use the string functions having a prefix of _t).
0
 

Author Comment

by:TongZ
ID: 40564699
@Qlemo
In my first post, I've stated that my project is already set to 'Use Multi-Byte Character Set'. Is that what you are referring to? If yes, then that's not an issue. Then, I guess your second suggestion would need to be looked at, could you elaborate a bit more on that? like give an example for how to pass the char* to my CString constructor?
0
 
LVL 71

Assisted Solution

by:Qlemo
Qlemo earned 200 total points
ID: 40581436
Sorry, did not see the MBCS part.

I cannot see what should be wrong, and need to fall back to basics.
Are you certain the exception is thrown in the CString constructor? The only way this might happen is if you forget the trailing NULL, and CString is trying to read past the allocated memory area.
It is always a good idea to use memset to fill the string buffer with zeros before using it.
And to make sure your lastindex is always less than the amount of chars you allocated. This means in your example above that each line needs to be less than 2048 characters, to allow [2047] to be '\0'.
You can also force the usage of MBCS CString by using CStringA instead.
0
 

Accepted Solution

by:
TongZ earned 0 total points
ID: 40587857
@Qlemo
I think setting the null char at the end is important, so I could take that as partial answer. However, what really resolved my problem was that I switched to use an instance of the MFC's CStdioFile class to ReadString, which automatically get to the end of line, hence relieve me the pain of doing this manually.
0
 

Author Closing Comment

by:TongZ
ID: 40596681
The objective of the initial question was to be able to use a dynamic string to read a file one line at a time. The best solution uses the CStdioFile class to achieve that reliably. However, I felt that Qlemo's comment is very informative as well. Hence, both should be accepted as answers.
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

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

Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
XMind Plus helps organize all details/aspects of any project from large to small in an orderly and concise manner. If you are working on a complex project, use this micro tutorial to show you how to make a basic flow chart. The software is free when…
Starting up a Project

688 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