VC逐行读写日志文件(TXT)

来源:互联网 发布:java filereader 编辑:程序博客网 时间:2024/04/27 21:45

最近写一个日志读写控件的类,发现VC的CSdioFile的WriteString不能处理中文,虽然把字符集改成多字符形式可以解决这个问题,但这就破坏了程序的兼容性。所以我写了以下的方法,另外还可以用一咱本地化的方法解决WriteString不能输入中文方法,附最后(这个也可以使用UNICODE字符集)。

用流在写日志时,发现如果把"/r/n"直接写在字符串的尾部,则会造成乱码,其中的原因网上有很多说明,主要是标准库与WINDOWS的回车换行的机制略有差别。但只要写到另一行重起就没有问题了。

/*****************************************************************************
*                         写入日志文件
* 函 数 名:WriteLogTxt
* 功    能 写入日志
* 说    明:
* 参    数:
* 创 建 人:fjf
* 创建时间:2009-09-09
* 修 改 人:
* 修改时间:
*****************************************************************************/

bool CConLog::WriteLogTxt(CString key, CString time, CString value)
{
//读写文件全名
if (m_sFullName == _T(""))
{
   AfxMessageBox("请设置日志保存路径!");
   return FALSE;
}

//操作文件
try
{
   this->m_sfFile.Open(m_sFullName,CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
   m_sfFile.SeekToEnd();
   this->m_sfFile.WriteString(time + _T("/r/n"));
   this->m_sfFile.WriteString(value + _T("/r/n"));
   this->m_sfFile.Close();
}
catch (CFileException &e)
{
   CString error;
   error.Format(_T("%d"),e.m_cause);
   AfxMessageBox(_T("无法写入文件!错误码:" + error));
   return false;
}

return true;
}

/*****************************************************************************
*                         写入日志文件-C++标准版(UNICODE)
* 函 数 名:WriteLogTxt
* 功    能 写入日志
* 说    明:
* 参    数:
* 创 建 人:fjf
* 创建时间:2009-09-10
* 修 改 人:
* 修改时间:
*****************************************************************************/

bool CConLog::WriteLogTxt(CString time, CString value)
{
//定义写入字符数组
CString tmp = time + value;

//定义输出流
ofstream oFile;
oFile.open(m_sFullName.GetBuffer(MAX_PATH),ios::app|ios::binary);
    oFile.seekp(0,ios::end);//回到文件末尾

//写入文件流
if (oFile.is_open())
{

//下面蓝色部分解决了CHAR[]写入的问题,不用再做拷贝了,增加了安全性
  
oFile.write(tmp.GetBuffer(tmp.GetLength()), tmp.GetLength());
   oFile.write(_T("/r/n"), 2);   //写在一起会产生乱码

}
else
{
   oFile.close();
   return false;
}

oFile.close();

return true;
}

/*****************************************************************************
*                         读取日志
* 函 数 名:ReadLogTxt
* 功    能 读取日志内容
* 说    明:
* 参    数:key:读取键
*            rlist:日志列表控件
* 创 建 人:fjf
* 创建时间:2009-09-09
* 修 改 人:
* 修改时间:
*****************************************************************************/

CString CConLog::ReadLogTxt(CString key,CListCtrl&rlist)
{
//读写文件全名
CString value ;//返回值
CString myStr = _T("");
CFileException e;

try
{
   //打开文档
   if (!this->m_sfFile.Open(m_sFullName,CFile::modeCreate | CFile::modeNoTruncate| CFile::modeRead ,&e))
   {
    CString error;
    error.Format(_T("%d"),e.m_cause);
    AfxMessageBox(_T("没有日志读取文件!错误码:" + error));

    return _T("");
   }

   while (this->m_sfFile.ReadString(value))
   {
    //读入并保存字符
    myStr = value;
    this->m_sfFile.ReadString(value);
    myStr += value;

    //添加项目
    rlist.InsertItem(0,_T(""),1);
    rlist.SetItemText(0,1,myStr);
   }
   //关闭文件句柄
   this->m_sfFile.Close();
}
catch (CFileException &e)
{
   CString error;
   error.Format(_T("%d"),e.m_cause);
   AfxMessageBox(_T("无法读取文件!错误码:" + error));
   return _T("");
}
return myStr;
}

/*****************************************************************************
*                         读取日志-C++标准版(UNICODE)
* 函 数 名:ReadLogTxt
* 功    能 读取日志内容
* 说    明:
* 参    数:rlist:日志列表控件
* 创 建 人:fjf
* 创建时间:2009-09-10
* 修 改 人:
* 修改时间:
*****************************************************************************/

CString CConLog::ReadLogTxt(CListCtrl & rlist)
{
// TODO: 在此添加控件通知处理程序代码
const int MAX = 80;
char txt[MAX];

ifstream iFile;//标准输入流
iFile.open(m_sFullName.GetBuffer(MAX),ios::binary);
iFile.seekg (0, ios::beg);//回到文件头

//读取文件
if (iFile.is_open())
{
   CString tmp;
   while(iFile)
   {
    iFile.getline(txt, MAX);
    CString str(txt);   

   }
   //CString str(txt.c_str());
     iFile.close();//关闭文件流
   return tmp;
}
else
{
   iFile.close();
   return _T("");
}

}

 

 

处理后可以使用WriteString方法,在VS2008测试成功。

#include <locale>//头文件

void Ctestvc2008Dlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
   CStdioFile m_sfFile;
CString m_sFullName = _T("C://Log.txt");
CString time = _T("2009-09-09");
CString value = _T("这是一个错误码");
try
{
   m_sfFile.Open(m_sFullName,CFile::modeCreate |CFile::modeNoTruncate | CFile::modeWrite);
   m_sfFile.SeekToEnd();
   m_sfFile.WriteString(time + _T(" ") + value);

char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );
   setlocale( LC_CTYPE, "chs" );//设定

   m_sfFile.WriteString(value);//正常写入
   setlocale( LC_CTYPE, old_locale );
   free( old_locale );//还原区域设定

   m_sfFile.Close();
}
catch (CFileException &e)
{
   CString error;
   error.Format(_T("%d"),e.m_cause);
   AfxMessageBox(_T("无法写入文件!错误码:" + error));
   return ;
}

return ;
}

void Ctestvc2008Dlg::OnBnClickedButton2()
{
// TODO: 在此添加控件通知处理程序代码
   //读写文件全名
   const int nBufSize = 512;
        TCHAR chBuf[nBufSize];
        ZeroMemory(chBuf,nBufSize);

        //获取当前执行文件的路径。
        if (GetModuleFileName(NULL,chBuf,nBufSize))
        {
              //输出带文件名称路径。
              OutputDebugString(chBuf);
              OutputDebugString(_T("/r/n"));

              //获取文件路径。
              TCHAR* lpStrPath = chBuf;
              PathRemoveFileSpec(lpStrPath);
              OutputDebugString(lpStrPath);
              OutputDebugString(_T("/r/n"));
        }
CStdioFile m_sfFile;
CString value ;//返回值
CString myStr = _T("");
CString m_sFullName = _T("C://Log.txt");
   
try
{
   //打开文档
   m_sfFile.Open(m_sFullName,CFile::modeCreate | CFile::modeNoTruncate| CFile::modeRead );
   //char* old_locale = _strdup( setlocale(LC_CTYPE,NULL) );
   //setlocale( LC_CTYPE, "chs" );//设定
   while (m_sfFile.ReadString(value))
   {
    //读入并保存字符
    myStr = value;
    m_sfFile.ReadString(value);
    myStr += value;

   }
   // setlocale( LC_CTYPE, old_locale );
   //free( old_locale );//还原区域设定
   //关闭文件句柄
   m_sfFile.Close();
}
catch (CFileException &e)
{
   CString error;
   error.Format(_T("%d"),e.m_cause);
   AfxMessageBox(_T("无法读取文件!错误码:" + error));
   return ;
}
}

原创粉丝点击