[分享] 用STL /C++ 逐行读取文件。

来源:互联网 发布:防身折叠式铁扇子淘宝 编辑:程序博客网 时间:2024/06/13 09:36

经常需要用到的逐行读取文件。

#include <iostream>#include <fstream>using namespace std;int main(){ifstream hFile( "D:\\atnt.txt", ios_base::in );if( !hFile )return 0;char szBuffer[512];while( hFile ){memset( szBuffer, 0, sizeof(szBuffer) );hFile.getline( szBuffer, 512 );if( hFile ){cout << szBuffer << endl;}}    return 1;}
嘿嘿,注释就没有必要了哦。需要注意的是512,根据自己的情况修改一下。