VC逐行读写文件(.TXT
时间:2010-02-05 16:22来源:未知 作者:zzl 点击: 1973
用流在写日志时,发现如果把"\r\n"直接写在字符串的尾部,则会造成乱码,其中的原因网上有很多说明,主要是标准库与WINDOWS的回车换行的机制略有差别。但只要写到另一行重起就没有
用流在写日志时,发现如果把"\r\n"直接写在字符串的尾部,则会造成乱码,其中的原因网上有很多说明,主要是标准库与WINDOWS的回车换行的机制略有差别。但只要写到另一行重起就没有问题了。
/*****************************************************************************
*                        写入日志文件-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;
}
/*****************************************************************************
*                        写入日志文件
* 名: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;
}
 
查看文章
 
 
VC逐行读写日志文件(TXT
2009-11-03 11:52
最近写一个日志读写控件的类,发现VCCSdioFileWriteString不能处理中文,虽然把字符集改成多字符形式可以解决这个问题,但这就破坏了程序的兼容性。所以我写了以下的方法,另外还可以用一咱本地化的方法解决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)
  {
    line(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:\\");
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()truncate读
{
// 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:\\");
   
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 ;
}
}
*****************************************************************************
*                        读取日志
* 名: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;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。