Solved

convert char* to MFC CString

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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Today, still in the boom of Apple, PC's and products, nearly 50% of the computer users use Windows as graphical operating systems. If you are among those users who love windows, but are grappling to keep the system's hard drive optimized, then you s…
Viewers will learn how to use the Hootsuite Dashboard.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

839 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