将CString写入到本地文件中,unicode

1、c语言

void WritePacket2File(CString packet_str)//packet_str为待写入的字符串
{
     CTime time = CTime::GetCurrentTime();
     CString time_str;
     time_str.Format(_T("%02d-%02d-%02d-%02d-%02d-%02d"),time.GetYear(),time.GetMonth(),time.GetDay(),time.GetHour(),time.GetMinute(),time.GetSecond());
     pFILE = _wfopen(this->savepath_udp_str,_T("ab+"));//savepath_udp_str为默认地址,初始值为_T("")
     if (pFILE == NULL)
     {
          savepath_udp_str =  _T("D:\\细水雾报文存储文件夹\\UDP\\");
          savepath_udp_str += time_str+_T(".txt");
          pFILE = _wfopen(savepath_udp_str,_T("ab+"));
     }
     fseek(pFILE,0,SEEK_END);
     if(ftell(pFILE)>1*1024*1024)//如果大于6M,重新建立文本
     {
          fclose(pFILE);
          savepath_udp_str =  _T("D:\\细水雾报文存储文件夹\\UDP\\");
          savepath_udp_str += time_str+_T(".txt");
          pFILE = _wfopen(savepath_udp_str,_T("ab+"));
     }
     USES_CONVERSION;
     char* packet_char;
     char* time_char;
     char backspace[2] = {0x20,0x00};//空格键
     packet_char = W2A(packet_str);
     time_char = W2A(time_str);
     fwrite(time_char,time_str.GetLength(),1,pFILE);  //写入时间
     fwrite(backspace,1,1,pFILE);      //写个空格键
     fwrite(packet_char,packet_str.GetLength(),1,pFILE); //写入包
     char ret[2];//向记事本中写入这个char数组可以实现换行
     ret[0] = 13;// \r
     ret[1] = 10;// \n
     fwrite(ret, 2, 1, pFILE);
     fclose(pFILE);
}

2、CFile

CStirng strInfo = _T("123你好");
USES_CONVERSION;
char* info = W2A(strInfo);
CFIle::Write(info,CStringA(strInfo).GetLength());

 

posted @ 2017-03-13 11:30  朱小勇  阅读(1315)  评论(0编辑  收藏  举报