Solved

convert char* to MFC CString

Posted on 2015-01-22
5
233 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
  • 3
  • 2
5 Comments
 
LVL 69

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 69

Assisted Solution

by:Qlemo
Qlemo earned 50 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

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

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

Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
All of the resources available today make learning a new digital media easier than ever-- if you know where to begin. This is a clear, simple guide to a few of the basic digital art mediums and how to begin learning them on your own.
Video by: Tony
This video teaches viewers how to export a project from Adobe Premiere Pro and the various file types involved.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

786 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