#include <stdio.h>
#include <fstream>
using namespace std;
bool FileGetLine(const char* szFileName)
{
bool fRet = false;
int iLineIndex = 0;
const int MAX_LINE_LEN = 512;
static char szLineData[MAX_LINE_LEN];
ifstream infile;
infile.open(szFileName);
if ( !infile.fail() )
{
while (true)
{
infile.getline(szLineData, MAX_LINE_LEN);
if ( infile.eof() )
{
fRet = true;
break;
}
else
{
printf("%03d:[%s]\n", ++iLineIndex, szLineData);
}
}
infile.close();
}
return fRet;
}
int main(void)
{
if ( true == FileGetLine("demo.txt") )
{
printf("ok\n");
}
else
{
printf("on!\n");
}
getchar();
return 0;
}
用C++的方式读取并显示文件的每一行
最新推荐文章于 2024-03-05 08:43:27 发布