搜档网
当前位置:搜档网 › VC逐行读写文件(.TXT)

VC逐行读写文件(.TXT)

VC逐行读写文件(.TXT)
VC逐行读写文件(.TXT)

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

最近写一个日志读写控件的类,发现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 //头文件

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 ;

}

}

********************************************************************* ********

* 读取日志

* 函数名: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;

}

相关主题