WriteString( ) MFC Function  
Author Message
jerrydoe





PostPosted: Visual C++ General, WriteString( ) MFC Function Top

Hello all,

I'm a newbie to this forum and appreciate if anyone of you has seen this problem before regarding the WriteString( ) in hte MFC class and provide some badly needed help. In my app I'm using the CStdioFile::WriteString( ) function. The string being passed into this function for printing into the log file, does have the entire SQL statement. But when the WriteString( ) function writes it into the log, the SQL statement is truncated at the first non-English character.

Bunch of developer whom i have spoken to think this should be a problem with either the WriteString( ) function, or with the way it is being used. We are trying to investigate if the usage of the CStdioFile class in the configurator code is correct or not. I have searched the internet and found a couple of instances where people have complained about the WriteString function not writing non-English characters, but the solution to this problem was not written anywhere.

The recommended file class to be used for unicode is CStdioFile. But in our case this didn't work because of problem with WriteString( ). So my question is: is there any other MFC class which can be used for this case

Any input will be greatly appreciated.



Visual C++11  
 
 
kuphryn





PostPosted: Visual C++ General, WriteString( ) MFC Function Top

example output and call to API

unicode

Kuphryn


 
 
Martin Richter





PostPosted: Visual C++ General, WriteString( ) MFC Function Top

Show us some lines of your code.
As kuphryn asked previously: Is this a unicode application

CStdioFile is a simple wrapper for fopen/fputs/fclose. So I see no problem in using it. You can try to use the plain CFile function, but the problem is somewhere else.



 
 
Ben Anderson MSFT





PostPosted: Visual C++ General, WriteString( ) MFC Function Top

As stated, make sure your project is configured to use unicode.

 
 
jerrydoe





PostPosted: Visual C++ General, WriteString( ) MFC Function Top

Kuphryn,Martin & Ben,

Thank you all for your prompt response. Yes it's Unicode. Here a code snippet

#include <afx.h>

class Log_Test
{
public:
CStdioFile Log_File;

Log_Test()
{

}
void test();
};

void Log_Test :: test()
{
CString update_string;
update_string = _T("update temp set column1 = \'\"Grupes\" ISDN iejungimo mokeseio nuolaidos\' where column2 = 1");
if(Log_File.Open(_T("c:\\test.txt"),
CFile::modeCreate | CFile::modeWrite ))
{
Log_File.WriteString(_T("===================================\n"));
Log_File.WriteString(_T("Status: Success \n"));
Log_File.WriteString( _T("Date: "));
CTime system_time = CTime::GetCurrentTime();
CString current_time = system_time.Format(_T("%d %B %Y %H:%M:%S"));
Log_File.WriteString(current_time);
Log_File.WriteString(_T("\nUser: "));
Log_File.WriteString(_T("\nSQL Statement:\n"));
Log_File.WriteString(_T(update_string));
Log_File.WriteString(_T("\n\n"));
Log_File.Close();
}

Here the string contains the right value, but while writing to the file using WriteString() fct, the statement is truncated in the file.

Thanks again guys. I really appreciate your help.


 
 
Viorel.





PostPosted: Visual C++ General, WriteString( ) MFC Function Top

If no other solutions, you can try this. If you need your log file in Unicode format, then you can open the file in binary mode:

Log_File.Open(..., CFile::modeCreate | CFile::modeWrite | CFile::typeBinary);

In this file strings are not truncated, but you have to put "\r\n" at the end of each strings.

If you need the file in Ansi format, then open the file in text mode:

Log_File.Open(..., CFile::modeCreate | CFile::modeWrite | CFile::typeText);

Then instead of WriteString, convert your Unicode string to Ansi format and write using Write. For simple conversion, you can try _bstr_t:

_bstr_t s = update_string;

const char * ptr = s;

Log_File.Write(ptr, lstrlenA(ptr));

This works well in our tests.

Use this way unless a better method is found.


 
 
jerrydoe





PostPosted: Visual C++ General, WriteString( ) MFC Function Top

Thanks a bunch Viorel. I will give it a shot and see what happens