本地化设置需要具备三个条件:
a. 语言代码 (Language Code)
b. 国家代码 (Country Code)
c. 编码(Encoding)
本地名字可以用下面这些部分来构造:
语言代码_国家代码.编码 比如(zh_CN.UTF-8, en_US等) locale的别名表见 /usr/lib/X11/locale/locale.alias(以Debian GNU/Linux为例)
setlocale语言字符串参考 本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ALENTAM/archive/2008/04/11/2281121.aspx 另外还有一种方法就是重新写CStdioFile的派生类CStdioFileEx(网上有)。 //好像C++中没有类能够读些Unicode格式的文本文件,所以我写了下面这个类。用法很简单,大家尝试几下就明白了。 #pragma once class CStdioFileEx: public CStdioFile
{
public:
CStdioFileEx();
CStdioFileEx( LPCTSTR lpszFileName, UINT nOpenFlags ); virtual BOOL Open( LPCTSTR lpszFileName, UINT nOpenFlags, CFileException* pError = NULL );
virtual BOOL ReadString(CString& rString);
BOOL ReadWideString(CStringW& rString);
BOOL ReadAnsiString(CStringA& rString);
virtual void WriteString(LPCTSTR lpsz);
void WriteWideString(LPCWSTR lpsz);
void WriteAnsiString(LPCSTR lpsz);
bool IsUnicodeFormat() {return m_bIsUnicodeText;}
unsigned long GetCharCount(); // Additional flag to allow Unicode text format writing
enum {modeWriteUnicode = 0x100000}; static bool IsFileUnicode(const CString& sFilePath); protected:
UINT PreprocessFlags(const CString& sFilePath, UINT& nOpenFlags); bool m_bIsUnicodeText;
}; //。cpp文件
#include "stdafx.h"
#include "StdioFileEx.h" //在UCS 编码中有一个叫做"ZERO WIDTH NO-BREAK SPACE"的字符,它的编码是FEFF。而FFFE在UCS中是不存在的字符,
//所以不应该出现在实际传输中。UCS规范建议我们在传输字节流前,先传输字符"ZERO WIDTH NO-BREAK SPACE"。这样
//如果接收者收到FEFF,就表明这个字节流是Big-Endian的;如果收到FFFE,就表明这个字节流Little-Endian的。
//因此字符"ZERO WIDTH NO-BREAK SPACE"又被称作BOM。
//UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字符"ZERO WIDTH NO-BREAK SPACE"的UTF-8编码是
//EF BB BF。所以如果接收者收到以EF BB BF开头的字节流,就知道这是UTF-8编码了。
//Windows就是使用BOM来标记文本文件的编码方式的。
//有些老的浏览器和文本编辑器不支持BOM。
#define UNICODE_BOM 0xFEFF//Unicode "byte order mark" which goes at start of file CStdioFileEx::CStdioFileEx(): CStdioFile()
{
m_bIsUnicodeText = false;
} CStdioFileEx::CStdioFileEx(LPCTSTR lpszFileName,UINT nOpenFlags)
:CStdioFile(lpszFileName, PreprocessFlags(lpszFileName, nOpenFlags))
{
} BOOL CStdioFileEx::Open(LPCTSTR lpszFileName,UINT nOpenFlags,CFileException* pError /*=NULL*/)
{
PreprocessFlags(lpszFileName, nOpenFlags); return CStdioFile::Open(lpszFileName, nOpenFlags, pError);
} BOOL CStdioFileEx::ReadString(CString& rString)
{
#ifdef _UNICODE
return ReadWideString(rString);
#else
return ReadAnsiString(rString);
#endif
} BOOL CStdioFileEx::ReadWideString(CStringW& rString)
{
_ASSERTE(m_pStream);
rString = L""; // empty string without deallocating if(m_bIsUnicodeText)
{
// If at position 0, discard byte-order mark before reading
if(GetPosition() == 0)
{
wchar_t bom;
Read(&bom, sizeof(wchar_t));
}
const int nMaxSize = 128;
LPWSTR lpsz = rString.GetBuffer(nMaxSize);
LPWSTR lpszResult;
int nLen = 0;
for (;;)
{
lpszResult = fgetws(lpsz, nMaxSize+1, m_pStream);
rString.ReleaseBuffer(); // handle error/eof case
if (lpszResult == NULL && !feof(m_pStream))
{
Afx_clearerr_s(m_pStream);
AfxThrowFileException(CFileException::genericException, _doserrno,
m_strFileName);
} // if string is read completely or EOF
if (lpszResult == NULL ||
(nLen = (int)lstrlenW(lpsz)) < nMaxSize ||
lpsz[nLen-1] == '\n')
break; nLen = rString.GetLength();
lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
}
//remove crlf if exist.
nLen = rString.GetLength();
if (nLen > 1 && rString.Mid(nLen-2) == L"\r\n")
{
rString.GetBufferSetLength(nLen-2);
}
return rString.GetLength() > 0;
}
else
{
CStringA ansiString;
BOOL bRetval = ReadAnsiString(ansiString);
//setlocale(LC_ALL, "chs_chn.936");//no need
rString = ansiString;
return bRetval;
}
} BOOL CStdioFileEx::ReadAnsiString(CStringA& rString)
{
_ASSERTE(m_pStream);
rString = ""; // empty string without deallocating if(!m_bIsUnicodeText)
{
const int nMaxSize = 128;
LPSTR lpsz = rString.GetBuffer(nMaxSize);
LPSTR lpszResult;
int nLen = 0;
for (;;)
{
lpszResult = fgets(lpsz, nMaxSize+1, m_pStream);
rString.ReleaseBuffer(); // handle error/eof case
if (lpszResult == NULL && !feof(m_pStream))
{
Afx_clearerr_s(m_pStream);
AfxThrowFileException(CFileException::genericException, _doserrno,
m_strFileName);
} // if string is read completely or EOF
if (lpszResult == NULL ||
(nLen = (int)lstrlenA(lpsz)) < nMaxSize ||
lpsz[nLen-1] == '\n')
break; nLen = rString.GetLength();
lpsz = rString.GetBuffer(nMaxSize + nLen) + nLen;
}
//remove crlf if exist.
nLen = rString.GetLength();
if (nLen > 1 && rString.Mid(nLen-2) == "\r\n")
{
rString.GetBufferSetLength(nLen-2);
}
return rString.GetLength() > 0;
}
else
{
CStringW wideString;
BOOL bRetval = ReadWideString(wideString);
//setlocale(LC_ALL, "chs_chn.936");//no need
rString = wideString;
return bRetval;
}
} // Purpose: Writes string to file either in Unicode or multibyte, depending on whether the caller specified the
// CStdioFileEx::modeWriteUnicode flag. Override of base class function.
void CStdioFileEx::WriteString(LPCTSTR lpsz)
{
#ifdef _UNICODE
WriteWideString(lpsz);
#else
WriteAnsiString(lpsz);
#endif
} void CStdioFileEx::WriteWideString(LPCWSTR lpsz)
{
ASSERT(lpsz != NULL); if (lpsz == NULL)
{
AfxThrowInvalidArgException();
}
if(m_bIsUnicodeText)
{
ASSERT(m_pStream != NULL);
// If writing Unicode and at the start of the file, need to write byte mark
if(GetPosition() == 0)
{
wchar_t cBOM = (wchar_t)UNICODE_BOM;
CFile::Write(&cBOM, sizeof(wchar_t));
}
if (fputws(lpsz, m_pStream) == _TEOF)
AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName);
}
else
{
USES_CONVERSION;
WriteAnsiString(CW2A(lpsz));
}
} void CStdioFileEx::WriteAnsiString(LPCSTR lpsz)
{
ASSERT(lpsz != NULL); if (lpsz == NULL)
{
AfxThrowInvalidArgException();
}
if(!m_bIsUnicodeText)
{
ASSERT(m_pStream != NULL);
if (fputs(lpsz, m_pStream) == _TEOF)
AfxThrowFileException(CFileException::diskFull, _doserrno, m_strFileName);
}
else
{
USES_CONVERSION;
WriteWideString(CA2W(lpsz));
}
} UINT CStdioFileEx::PreprocessFlags(const CString& sFilePath, UINT& nOpenFlags)
{
m_bIsUnicodeText = false; // If we have writeUnicode we must have write or writeRead as well
if (nOpenFlags & CStdioFileEx::modeWriteUnicode)
{
ASSERT(nOpenFlags & CFile::modeWrite || nOpenFlags & CFile::modeReadWrite);
m_bIsUnicodeText = true;
}
// If reading in text mode and not creating...
else if (nOpenFlags & CFile::typeText && !(nOpenFlags & CFile::modeCreate) && !(nOpenFlags & CFile::modeWrite ))
{
m_bIsUnicodeText = IsFileUnicode(sFilePath);
} //如果要读写Unicode格式的文本文件, 必须切换到typeBinary方式, 因为这会影响fputws/fgetws的工作方式(具体情况参考MSDN)。
if (m_bIsUnicodeText)
{
nOpenFlags &= ~(CFile::typeText);
nOpenFlags |= CFile::typeBinary;
} return nOpenFlags;
} // Purpose: Determines whether a file is Unicode by reading the first character and detecting
// whether it's the Unicode byte marker.
bool CStdioFileEx::IsFileUnicode(const CString& sFilePath)
{
CFile file;
wchar_t cFirstChar;
CFileException exFile; bool bIsUnicode = false;
// Open file in binary mode and read first character
if (file.Open(sFilePath, CFile::typeBinary | CFile::modeRead, &exFile))
{
// If byte is Unicode byte-order marker, let's say it's Unicode
if (file.Read(&cFirstChar, sizeof(wchar_t)) > 0 && cFirstChar == (wchar_t)UNICODE_BOM)
{
bIsUnicode = true;
} file.Close();
}
else
{
// Handle error here if you like
} return bIsUnicode;
} unsigned long CStdioFileEx::GetCharCount()
{
int nCharSize;
unsigned long nByteCount, nCharCount = 0; if (m_pStream)
{
// Get size of chars in file
nCharSize = m_bIsUnicodeText ? sizeof(wchar_t): sizeof(char); // If Unicode, remove byte order mark from count
nByteCount = (unsigned long)GetLength(); if (m_bIsUnicodeText)
{
nByteCount = nByteCount - sizeof(wchar_t);
} // Calc chars
nCharCount = (nByteCount / nCharSize);
} return nCharCount;
}

C++ writestring 为什么不能写进中文 CStdioFile向无法向文本中写入中文【二】的更多相关文章

  1. PHP往mysql数据库中写入中文失败

    该类问题解决办法就是 在建立数据库连接之后,将该连接的编码方式改为中文. 代码如下: $linkID=@mysql_connect("localhost","root&q ...

  2. Python: 在CSV文件中写入中文字符

    0.2 2016.09.26 11:28* 字数 216 阅读 8053评论 2喜欢 5 最近一段时间的学习中发现,Python基本和中文字符杠上了.如果能把各种编码问题解决了,基本上也算对Pytho ...

  3. Linux学习笔记之ubuntu如何在vi中写入中文注释

    点击左边设置system settings,再点击Language Suppotr 点击Remind Me Later 选择ibus 然后关闭,在终端写入ibus-setup,弹出设置框,选择INPU ...

  4. qt 文本中显示中文

    QTextCodec *codec = QTextCodec::codecForName("utf8");QTextCodec::setCodecForLocale(codec); ...

  5. java向mysql中写入中文出现乱码

    乱码的原因有很多,我遇到的原因是url配置的问题,解决方案: 将: jdbc.url=jdbc:mysql://localhost:3306/XXXX?useUnicode=true&char ...

  6. CStdioFile的Writestring无法写入中文的问题

    解决UNICODE字符集下CStdioFile的Writestring无法写入中文的问题 2009-12-01 23:11 以下代码文件以CStdioFile向无法向文本中写入中文(用notepad. ...

  7. 使用cstdiofile在vs2010中无法写入中文的问题

    在VC2010环境下, 以下代码无法实现使用CStdioFile向文本文件中写入中文(用notepad.exe查看不到写入的中文) CStdioFile file; file.Open(…); fil ...

  8. mysql5.7中解决中文乱码的问题

    在使用mysql5.7时,会发现通过web端向数据库中写入中文后会出现乱码,但是在数据库中直接操作SQL语句插入数据后中文就显示正常,这个问题怎么解决呢?此处不对mysql的数据编码过程和原理进行讲解 ...

  9. 如何在Ubuntu中安装中文输入法

    在使用ubuntu系统时,有的时候总觉得英文输入法不方便操作,总希望能有中文输入法可以辅助操作,那怎样才能在ubuntu中安装中文输入法呢?下面有一种简单的方法可以安装中文输入法. 如何在ubuntu ...

随机推荐

  1. CSS Outline(轮廓)

    CSS Outline(轮廓) 一.CSS 轮廓(outline) 轮廓(outline)是绘制于元素周围的一条线,位于边框边缘的外围,可起到突出元素的作用. CSS outline 属性规定元素轮廓 ...

  2. 20145230熊佳炜《网络对抗》实验八:WEB基础

    20145230熊佳炜<网络对抗>实验八:WEB基础 实验目标 Web前端HTML:能正常安装.启停Apache.理解HTML,理解表单,理解GET与POST方法,编写一个含有表单的HTM ...

  3. android与linux之间的关系

    篇一(system/core/init/init.c): 对Android感兴趣的朋友都知道,Android系统是建立在Linux内核之上的.那么Linux内核和Android什么关系?Linux内核 ...

  4. Java 线程池Future和FutureTask

    Future表示一个任务的周期,并提供了相应的方法来判断是否已经完成或者取消,以及获取任务的结果和取消任务. Future接口源码: public interface Future<V> ...

  5. Import SQL into MySQL with a progress meter

    There is nice tool called pv # On Ubuntu/Debian system $ sudo apt-get install pv # On Redhat/CentOS ...

  6. AVL模板

    感谢此博客 #include <bits/stdc++.h> #define pb push_back #define mp make_pair #define de(x) cout &l ...

  7. 盘点SQL on Hadoop中用到的主要技术

    转载自:http://sunyi514.github.io/2014/11/15/%E7%9B%98%E7%82%B9sql-on-hadoop%E4%B8%AD%E7%94%A8%E5%88%B0% ...

  8. 医疗数据库 Caché 开发笔记

    目前所知的 Caché 是应用在医院信息系统(即 HIS),据说在欧美医疗卫生行业,Caché 占了 70% 的市场份额.国内的东华软件就是采用 Caché 数据库,东华软件在国内医院市场占有率大致为 ...

  9. 【Python】常用排序算法的python实现和性能分析

    作者:waterxi 原文链接 背景 一年一度的换工作高峰又到了,HR大概每天都塞几份简历过来,基本上一天安排两个面试的话,当天就只能加班干活了.趁着面试别人的机会,自己也把一些基础算法和一些面试题整 ...

  10. 对reducers 理解

    var reducers = { totalInEuros : function(state, item) { return state.euros += item.price * 0.8974243 ...