谁能给我一段代码是将str1里的中文内容写入111.txt文件,然后再从111.txt里读出来放到CString str2里...谢谢各们高手了
8 个解决方案
#1
CStdioFile fLogList;
CString logstring=_T("新年快乐");
fLogList.Open(_T("LogList.txt"), CFile::modeCreate|CFile::modeReadWrite);
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL
pTempChar=(char*)malloc(ansiCount*sizeof(char
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
logstring = pTempChar;
fLogList.WriteString(logstring);
free(pTempChar);
我用这段代码写进去英文可以,中文就是空的了;
CString logstring=_T("新年快乐");
fLogList.Open(_T("LogList.txt"), CFile::modeCreate|CFile::modeReadWrite);
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL
pTempChar=(char*)malloc(ansiCount*sizeof(char
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
logstring = pTempChar;
fLogList.WriteString(logstring);
free(pTempChar);
我用这段代码写进去英文可以,中文就是空的了;
#2
按俺的习惯,都是用C语言中的fwrite来写的,为什么不直接将pTempChar写进去?
#3
FILE* pFile;
pFile = fopen("LogList.txt", "w");
CString logstring=_T("新年快乐");
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL);
pTempChar=(char*)malloc(ansiCount*sizeof(char));
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
fwrite(pTempChar,sizeof(char),ansiCount,pFile);
free(pTempChar);
fclose(pFile);
pFile = fopen("LogList.txt", "w");
CString logstring=_T("新年快乐");
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL);
pTempChar=(char*)malloc(ansiCount*sizeof(char));
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
fwrite(pTempChar,sizeof(char),ansiCount,pFile);
free(pTempChar);
fclose(pFile);
#4
fwrite(pTempChar,sizeof(char),ansiCount,pFile);
改成
fwrite(pTempChar,sizeof(char),ansiCount - 1,pFile);
不然会多写入一个空格
为什么会这样我还没查到,望后面有高手能解答
改成
fwrite(pTempChar,sizeof(char),ansiCount - 1,pFile);
不然会多写入一个空格
为什么会这样我还没查到,望后面有高手能解答
#5
把如下代码改一下:
fLogList.WriteString(logstring);
改为:
fLogList.WriteString(pTempChar);
==================================
补充:
楼主应该说明一下你的开发环境、工程配置等信息:
如:是否是UNICODE工程?
#6
给你一段完全Unicode的。
CString str1 = L"新年快乐";
CString str2;
DWORD bytes;
WCHAR buffer[100];
HANDLE file = ::CreateFileW(L"111.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (file != INVALID_HANDLE_VALUE)
{
::WriteFile(file, L"\xfeff", sizeof(WCHAR), &bytes, NULL);
::WriteFile(file, str1.GetBuffer(), str1.GetLength()*sizeof(WCHAR), &bytes, NULL);
::SetFilePointer(file, 0, NULL, FILE_BEGIN);
::ReadFile(file, buffer, sizeof(buffer)-sizeof(WCHAR), &bytes, NULL);
::CloseHandle(file);
buffer[bytes/sizeof(WCHAR)] = 0;
str2 = buffer;
}
#7
有点小问题,更正一下:
CString str1 = L"新年快乐";
CString str2;
DWORD bytes;
WCHAR buffer[100];
HANDLE file = ::CreateFileW(L"111.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (file != INVALID_HANDLE_VALUE)
{
::WriteFile(file, L"\xfeff", sizeof(WCHAR), &bytes, NULL);
::WriteFile(file, str1.GetBuffer(), str1.GetLength()*sizeof(WCHAR), &bytes, NULL);
::SetFilePointer(file, sizeof(WCHAR), NULL, FILE_BEGIN);
::ReadFile(file, buffer, sizeof(buffer)-sizeof(WCHAR), &bytes, NULL);
::CloseHandle(file);
buffer[bytes/sizeof(WCHAR)] = 0;
str2 = buffer;
}
#8
我在标题里已经说明了要在UNICODE环境下开发了.........
#1
CStdioFile fLogList;
CString logstring=_T("新年快乐");
fLogList.Open(_T("LogList.txt"), CFile::modeCreate|CFile::modeReadWrite);
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL
pTempChar=(char*)malloc(ansiCount*sizeof(char
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
logstring = pTempChar;
fLogList.WriteString(logstring);
free(pTempChar);
我用这段代码写进去英文可以,中文就是空的了;
CString logstring=_T("新年快乐");
fLogList.Open(_T("LogList.txt"), CFile::modeCreate|CFile::modeReadWrite);
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL
pTempChar=(char*)malloc(ansiCount*sizeof(char
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
logstring = pTempChar;
fLogList.WriteString(logstring);
free(pTempChar);
我用这段代码写进去英文可以,中文就是空的了;
#2
按俺的习惯,都是用C语言中的fwrite来写的,为什么不直接将pTempChar写进去?
#3
FILE* pFile;
pFile = fopen("LogList.txt", "w");
CString logstring=_T("新年快乐");
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL);
pTempChar=(char*)malloc(ansiCount*sizeof(char));
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
fwrite(pTempChar,sizeof(char),ansiCount,pFile);
free(pTempChar);
fclose(pFile);
pFile = fopen("LogList.txt", "w");
CString logstring=_T("新年快乐");
int ansiCount;
char *pTempChar;
ansiCount=WideCharToMultiByte(CP_ACP,0,logstring,-1,NULL,0,NULL,NULL);
pTempChar=(char*)malloc(ansiCount*sizeof(char));
memset(pTempChar,0,ansiCount);
WideCharToMultiByte(CP_ACP,0,logstring,-1,pTempChar,ansiCount,NULL,NULL);
fwrite(pTempChar,sizeof(char),ansiCount,pFile);
free(pTempChar);
fclose(pFile);
#4
fwrite(pTempChar,sizeof(char),ansiCount,pFile);
改成
fwrite(pTempChar,sizeof(char),ansiCount - 1,pFile);
不然会多写入一个空格
为什么会这样我还没查到,望后面有高手能解答
改成
fwrite(pTempChar,sizeof(char),ansiCount - 1,pFile);
不然会多写入一个空格
为什么会这样我还没查到,望后面有高手能解答
#5
把如下代码改一下:
fLogList.WriteString(logstring);
改为:
fLogList.WriteString(pTempChar);
==================================
补充:
楼主应该说明一下你的开发环境、工程配置等信息:
如:是否是UNICODE工程?
#6
给你一段完全Unicode的。
CString str1 = L"新年快乐";
CString str2;
DWORD bytes;
WCHAR buffer[100];
HANDLE file = ::CreateFileW(L"111.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (file != INVALID_HANDLE_VALUE)
{
::WriteFile(file, L"\xfeff", sizeof(WCHAR), &bytes, NULL);
::WriteFile(file, str1.GetBuffer(), str1.GetLength()*sizeof(WCHAR), &bytes, NULL);
::SetFilePointer(file, 0, NULL, FILE_BEGIN);
::ReadFile(file, buffer, sizeof(buffer)-sizeof(WCHAR), &bytes, NULL);
::CloseHandle(file);
buffer[bytes/sizeof(WCHAR)] = 0;
str2 = buffer;
}
#7
有点小问题,更正一下:
CString str1 = L"新年快乐";
CString str2;
DWORD bytes;
WCHAR buffer[100];
HANDLE file = ::CreateFileW(L"111.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, 0, NULL);
if (file != INVALID_HANDLE_VALUE)
{
::WriteFile(file, L"\xfeff", sizeof(WCHAR), &bytes, NULL);
::WriteFile(file, str1.GetBuffer(), str1.GetLength()*sizeof(WCHAR), &bytes, NULL);
::SetFilePointer(file, sizeof(WCHAR), NULL, FILE_BEGIN);
::ReadFile(file, buffer, sizeof(buffer)-sizeof(WCHAR), &bytes, NULL);
::CloseHandle(file);
buffer[bytes/sizeof(WCHAR)] = 0;
str2 = buffer;
}
#8
我在标题里已经说明了要在UNICODE环境下开发了.........