How to convert a CString object to char array in VC++ MFC
-
Hi, Is there any MFC method to convert CString object to char array? please tell me how to do this. Thanks in advance. Regards, jo
hi
-
Hi, Is there any MFC method to convert CString object to char array? please tell me how to do this. Thanks in advance. Regards, jo
hi
It is not at all good to convert a CString to char array unless you are sure that it doesn't contain any UNICODE character. well there a lot of methods one of them is CString csdata = "asdsadasdda"; char* cdata = csdata.GetBuffer(); //When you have done all your needs, use ReleaseBuffer(). csdata.ReleaseBuffer();
Величие не Бога может быть недооценена.
modified on Monday, January 25, 2010 1:36 AM
-
Hi, Is there any MFC method to convert CString object to char array? please tell me how to do this. Thanks in advance. Regards, jo
hi
CString s(_T("Hello World"));
TCHAR* pStr = new TCHAR[s.GetLength() + 1];
lstrcpy(pStr, s);
_putts(pStr);
delete [] pStr; -
CString s(_T("Hello World"));
TCHAR* pStr = new TCHAR[s.GetLength() + 1];
lstrcpy(pStr, s);
_putts(pStr);
delete [] pStr;Thanks joe :) i have one more problem. please check the code below here pdmFile,pdmSecFile are two CFile objs.
CString strLine; totlen = pdmFile.GetLength(); for(int i = 0; i < totlen; i++) { UINT lBytesRead = pdmFile.Read(ch,1); if(ch\[0\] == '\\n') { int totl = strLine.GetLength(); TCHAR\* pStr = new TCHAR\[strLine.GetLength() + 1\]; lstrcpy(pStr, strLine); pdmSecFile.Write(pStr, totl+1); delete \[\] pStr; pdmSecFile.Flush(); strLine.ReleaseBuffer(); strLine.Empty(); } strLine.AppendChar(ch\[0\]); } pdmFile.Close(); pdmSecFile.Close();
iam trying to read each line from the first file and writing to second file. i am getting the out put but after every character its printing NULL character. please check the code and give me some solution to get the desired output. Thanks in advance! Regards, jo
hi
-
It is not at all good to convert a CString to char array unless you are sure that it doesn't contain any UNICODE character. well there a lot of methods one of them is CString csdata = "asdsadasdda"; char* cdata = csdata.GetBuffer(); //When you have done all your needs, use ReleaseBuffer(). csdata.ReleaseBuffer();
Величие не Бога может быть недооценена.
modified on Monday, January 25, 2010 1:36 AM
Adam Roderick J 09 wrote:
char* cdata = csdata.GetBuffer();
Calling GetBuffer is not a good solution in this case, even if you call ReleaseBuffer afterward. There's no need to use GetBuffer because CString already defines casting operators (which returns the same type of what GetBuffer returns, so it depends on the UNICODE settings). Using GetBuffer is very bad practice and should be avoided.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++ -
Thanks joe :) i have one more problem. please check the code below here pdmFile,pdmSecFile are two CFile objs.
CString strLine; totlen = pdmFile.GetLength(); for(int i = 0; i < totlen; i++) { UINT lBytesRead = pdmFile.Read(ch,1); if(ch\[0\] == '\\n') { int totl = strLine.GetLength(); TCHAR\* pStr = new TCHAR\[strLine.GetLength() + 1\]; lstrcpy(pStr, strLine); pdmSecFile.Write(pStr, totl+1); delete \[\] pStr; pdmSecFile.Flush(); strLine.ReleaseBuffer(); strLine.Empty(); } strLine.AppendChar(ch\[0\]); } pdmFile.Close(); pdmSecFile.Close();
iam trying to read each line from the first file and writing to second file. i am getting the out put but after every character its printing NULL character. please check the code and give me some solution to get the desired output. Thanks in advance! Regards, jo
hi
First, there is no need to do the copy operation.
pdmSecFile.Write((LPCTSTR) pStr, strLine.GetLength() * sizeof(TCHAR));
is sufficient (which also elminates theint totl =
line.) Second, you are writing out the NULL character with totl+1. Third, the Flush is not needed. Fourth, ReleaseBuffer is not needed. Fifth, you should append the char immediately, and then test. Otherwise the last return never gets written. Sixth, the last line may never get written if it doesn't end in a return. Seventh, just use CStdioFile and have it read the entire string for you! -
Adam Roderick J 09 wrote:
char* cdata = csdata.GetBuffer();
Calling GetBuffer is not a good solution in this case, even if you call ReleaseBuffer afterward. There's no need to use GetBuffer because CString already defines casting operators (which returns the same type of what GetBuffer returns, so it depends on the UNICODE settings). Using GetBuffer is very bad practice and should be avoided.
Cédric Moonen Software developer
Charting control [v3.0] OpenGL game tutorial in C++thanks for correcting me. :-D
Величие не Бога может быть недооценена.
-
First, there is no need to do the copy operation.
pdmSecFile.Write((LPCTSTR) pStr, strLine.GetLength() * sizeof(TCHAR));
is sufficient (which also elminates theint totl =
line.) Second, you are writing out the NULL character with totl+1. Third, the Flush is not needed. Fourth, ReleaseBuffer is not needed. Fifth, you should append the char immediately, and then test. Otherwise the last return never gets written. Sixth, the last line may never get written if it doesn't end in a return. Seventh, just use CStdioFile and have it read the entire string for you!Thanks joe for your comments. i changed code like this to eliminate char array.
CFile pdmFile; CFile pdmSecFile; pdmFile.Open(\_T("c:\\\\strucadv15\\\\Work\\\\testpdms\\\\mod\\\\testpdms.pdm"),CFile::modeRead); pdmSecFile.Open(\_T("c:\\\\strucadv15\\\\Work\\\\testpdms\\\\mod\\\\testpdms\_changed.pdm"),CFile::modeCreate | CFile::modeReadWrite); CString strLinee; totlen = pdmFile.GetLength(); for(int i = 0; i < totlen; i++) { UINT lBytesRead = pdmFile.Read(ch,1); if(ch\[0\] == '\\n') { pdmSecFile.Write(strLinee, strLinee.GetLength()); strLinee.Empty(); } strLinee.AppendChar(ch\[0\]); } pdmFile.Close(); pdmSecFile.Close();
but still i got the same output like, after every character its appending one NULL please give me some solution. :) Regards, Jo
hi
-
Thanks joe for your comments. i changed code like this to eliminate char array.
CFile pdmFile; CFile pdmSecFile; pdmFile.Open(\_T("c:\\\\strucadv15\\\\Work\\\\testpdms\\\\mod\\\\testpdms.pdm"),CFile::modeRead); pdmSecFile.Open(\_T("c:\\\\strucadv15\\\\Work\\\\testpdms\\\\mod\\\\testpdms\_changed.pdm"),CFile::modeCreate | CFile::modeReadWrite); CString strLinee; totlen = pdmFile.GetLength(); for(int i = 0; i < totlen; i++) { UINT lBytesRead = pdmFile.Read(ch,1); if(ch\[0\] == '\\n') { pdmSecFile.Write(strLinee, strLinee.GetLength()); strLinee.Empty(); } strLinee.AppendChar(ch\[0\]); } pdmFile.Close(); pdmSecFile.Close();
but still i got the same output like, after every character its appending one NULL please give me some solution. :) Regards, Jo
hi
-
totlen = pdmFile.GetLength();
<- This gives you no of chars in the file. So for loop iterates for each char and you are inserting null char: ->strLinee.AppendChar(ch[0]);
You should look for a new line char to get the no of lines in the file.Thanks Maya! :) what you said is correct... but i solved that problem in different way, I guess this is the easiest way. any how I am keeping that code here, it will be helpful for others. I used CStdioFile instead of CFile, that really simplifies my work.
CStdioFile pdmFile; CStdioFile pdmSecFile; pdmFile.Open(m\_sPdmFileName, CStdioFile::modeRead); pdmSecFile.Open(\_T("testpdms\_changed.pdm"),CStdioFile::modeCreate | CStdioFile::modeReadWrite); CString sKey; CString sValue; POSITION pos; while(pdmFile.ReadString(strLine)) { pos = mapingStrings.GetStartPosition(); while(pos != NULL) { mapingStrings.GetNextAssoc(pos, sKey, sValue); strLine.Replace(sKey,sValue); } pdmSecFile.WriteString(strLine); pdmSecFile.WriteString(\_T("\\n")); } pdmFile.Close(); pdmSecFile.Close();
Regards, Jo
hi
-
Hi, Is there any MFC method to convert CString object to char array? please tell me how to do this. Thanks in advance. Regards, jo
hi
hi .. Use _tcscpy() to copy from CString to TCHAR array; ex- CString csMystring = L"Hi all"; TCHAR tchChar[MAX_PATH]; memset(tchChar, 0x00, MAX_PATH); _tcscpy(tchChar, csMystring); Hope it will help. Thanks MChauhan
-
Thanks joe for your comments. i changed code like this to eliminate char array.
CFile pdmFile; CFile pdmSecFile; pdmFile.Open(\_T("c:\\\\strucadv15\\\\Work\\\\testpdms\\\\mod\\\\testpdms.pdm"),CFile::modeRead); pdmSecFile.Open(\_T("c:\\\\strucadv15\\\\Work\\\\testpdms\\\\mod\\\\testpdms\_changed.pdm"),CFile::modeCreate | CFile::modeReadWrite); CString strLinee; totlen = pdmFile.GetLength(); for(int i = 0; i < totlen; i++) { UINT lBytesRead = pdmFile.Read(ch,1); if(ch\[0\] == '\\n') { pdmSecFile.Write(strLinee, strLinee.GetLength()); strLinee.Empty(); } strLinee.AppendChar(ch\[0\]); } pdmFile.Close(); pdmSecFile.Close();
but still i got the same output like, after every character its appending one NULL please give me some solution. :) Regards, Jo
hi
Member 4399771 wrote:
after every character its appending one NULL
You are writing a UNICODE string to the file. Each AppendChar() converts the character to a UNICODE character and appends it to the string. During your write, you'll also note that only half the string is being written. (CString::GetLength() returns the number of characters, not the number of bytes.)
-
totlen = pdmFile.GetLength();
<- This gives you no of chars in the file. So for loop iterates for each char and you are inserting null char: ->strLinee.AppendChar(ch[0]);
You should look for a new line char to get the no of lines in the file.That's not correct. He reads a character at a time and then appends it. The problem is that the concatenation operation is likely creating a UNICODE string.