使用CStdioFile读取/写入中文文本文件出错的问题

本文介绍了一种解决C++环境下中文乱码的方法,通过设定语言区域为中文来确保在UNICODE编码下正确读取中文字符。文章提供了一个具体的类实现示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个是由编码、语言区域等问题导致。

解决方法为设定语言区域为中文,如下:

  1. #include <locale.h>   
  2. //CStdioFileChs UNICODE编译 读取中文汉字乱码解决办法   
  3.   
  4. class CStdioFileChs : public CStdioFile  
  5. {  
  6. private:  
  7.     char *old_locale;  
  8.   
  9. public:  
  10.     CStdioFileChs(const LPCTSTR path = NULL, UINT nOpenFlags = CStdioFile::modeWrite)  
  11.     {  
  12.         //设置语言为中文,否则在Unicode编码下读出中文字符为乱码   
  13.         old_locale=_strdup( setlocale(LC_CTYPE, NULL) );  
  14.         setlocale( LC_CTYPE,"chs");  
  15.         if ( NULL == path )  
  16.             return;  
  17.         Open( path, nOpenFlags);          
  18.     }  
  19.     ~CStdioFileChs()  
  20.     {  
  21.         setlocale( LC_CTYPE, old_locale ); //还原语言区域的设置   
  22.         free( old_locale );//还原区域设定   
  23.     }  
  24. };  
#include <locale.h>
//CStdioFileChs UNICODE编译 读取中文汉字乱码解决办法

class CStdioFileChs : public CStdioFile
{
private:
	char *old_locale;

public:
	CStdioFileChs(const LPCTSTR path = NULL, UINT nOpenFlags = CStdioFile::modeWrite)
	{
		//设置语言为中文,否则在Unicode编码下读出中文字符为乱码
		old_locale=_strdup( setlocale(LC_CTYPE, NULL) );
		setlocale( LC_CTYPE,"chs");
		if ( NULL == path )
			return;
		Open( path, nOpenFlags);		
	}
	~CStdioFileChs()
	{
		setlocale( LC_CTYPE, old_locale ); //还原语言区域的设置
		free( old_locale );//还原区域设定
	}
};

 

PendingPath := appFileDir + '\comp\common\extend\taskFiles\Pending\'; ProcessedPath := appFileDir + '\comp\common\extend\taskFiles\Processed\'; if DirectoryExists(PendingPath) = False then begin CreateDirectory(PChar(PendingPath), nil); end; if DirectoryExists(ProcessedPath) = False then begin CreateDirectory(PChar(ProcessedPath), nil); end; if FindFirst(PendingPath+'*.txt',faAnyFile,SearchRec) = 0 then begin repeat TaskID := ChangeFileExt(SearchRec.Name, ''); FilePath := PendingPath + SearchRec.Name; bsLogger.WriteLog('任务文件地址:'+FilePath); TaskFile :=TStringList.Create; try TaskFile.LoadFromFile(FilePath); inParams := TaskFile.Values['inParams']; // TaskValue := GetTaskValue(inParams); TaskValue := doGetPrinterStatus('DS-1870','0'); bsLogger.WriteLog('处理结果:'+TaskValue); TaskFile.Values['TaskValue'] := TaskValue; TaskFile.SaveToFile(FilePath); MoveFile(PChar(FilePath), PChar(ProcessedPath + SearchRec.Name)) finally TaskFile.Free; end; until FindNext(SearchRec) <> 0; FindClose(SearchRec); end; tmrTask.Enabled := True; TaskID := ''; 参考如上代码 修改void CDemoDlg::OnTimer(UINT_PTR nIDEvent) { if (nIDEvent == 1) { int iret = m_GetPrinterStatus("DS-1870", 0); // 获取当前时间 CTime currentTime = CTime::GetCurrentTime(); CString strTime = currentTime.Format(_T("%Y-%m-%d %H:%M:%S")); // 构造日志内容 CString strLog; strLog.Format(_T("[%s] Printer Status: %d\r\n"), strTime, iret); // 写入文件(自动创建+续CStdioFile file; if (file.Open(_T("GetPrinterStatus.txt"), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite | CFile::typeText)) { file.SeekToEnd(); // 定位到文件末尾 file.WriteString(strLog); file.Close(); } } CDialog::OnTimer(nIDEvent); } 使得可以从PendingPath读取txt文件,解出文件中的json的printerName和printerType,填到doGetPrinterStatus('DS-1870','0')的两个参数对应printerName和printerType,调用m_GetPrinterStatus后在txt文件中填TaskValue字段并写入文件到ProcessedPath
最新发布
03-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值