最近我写了一个小东东,需要利用文件结束的判定来结束文件的读取,但是我不知到怎样设置,好像在c语言里面就有所涉及这方面的内容,但是一时不记得怎样搞了,望知道着给于提示提示。
附录代码如下:
//注:思路是先读取,再分段,然后分析段,再连接,最后将符合条件着写入文件
//注:readfrom.n40内容如下
line1 tester1 0001 20071001224510 20071002083122 20060322
line1 tester2 0002 20071001224510 20071002083122 20060322
line1 tester3 0000 20071001224510 20071002083122 20060322
line1 tester4 0003 20071001224510 20071002083122 20060322
line1 tester5 0000 20071001224510 20071002083122 20060322
..........
//执行后writeto.txt内容如下:
line1 tester1 0001 20071001224510 20071002083122 20060322
line1 tester2 0002 20071001224510 20071002083122 20060322
line1 tester4 0003 20071001224510 20071002083122 20060322
.................
void CFilerwDlg::OnOK()
{
char* pFileNamer="readfrom.n40";//读文件名称
char* pFileNamew="writeto.txt"; //写文件名称
CString linestr; //按行读内容字符串
TRY
{
CStdioFile filer(pFileNamer, CFile::modeRead);
CStdioFile filew(pFileNamew, CFile::modeCreate|CFile::modeWrite);
for(int i=0; i<=100; i++)//我所问的问题就在这里,怎样判定文件结束,而不要用指定该文件的实际行数
{ //好像do-while比较方便,请高手提示提示
filer.ReadString(linestr); //read first
CString licode=linestr.Left(6); //get string 1
CString tecode=linestr.Mid(6, 8); //get string 2
CString ercode=linestr.Mid(14, 4); //get...
CString becode=linestr.Mid(19,15);
CString encode=linestr.Mid(34,15);
CString opcode=linestr.Mid(49, 9);
if(ercode!=_T("0000")) //set condition that error code
{ //fails to meet "0000".
CString total=licode+tecode+ercode+becode+encode+opcode+'\n';
filew.WriteString(total); //then write
}
}
filer.Close();
filew.Close();
}
CATCH(CFileException,e)
{
#ifdef _DEBUG
afxDump<<"File could not be opened"<<e->m_cause<<"\n";
#endif
}
END_CATCH
CDialog::OnOK();
}