Solved

convert char* to MFC CString

Posted on 2015-01-22
5
222 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 68

Expert Comment

by:Qlemo
Comment Utility
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
Comment Utility
@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 68

Assisted Solution

by:Qlemo
Qlemo earned 50 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is about my first experience with programming Arduino.
Healthcare organizations in the United States must adhere to the guidance of both the HIPAA (Health Insurance Portability and Accountability Act) and HITECH (Health Information Technology for Economic and Clinical Health Act) for securing and protec…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

771 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

11 Experts available now in Live!

Get 1:1 Help Now