文本文件的行长度问题
请问C++中如何获得文本文件的行长度的,请附上完整的程序,送上30分,谢谢!
[解决办法]
#include <fstream>
#include <string>
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
int len, i=1;
string line;
ifstream ifile( "text.txt ");
while(!ifile.eof())
{
getline(ifile, line); //读取一行
cout < < "文件第 " < <i < < "行的长度为 " < <line.length() < <endl; //line.length()求长度
i++;
}
system( "pause ");
return 0;
}
[解决办法]
Dev-C++
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
ifstream myfile;
myfile.open( "c:\\1.txt ");
if(!myfile)
cerr < < "file is not open! " < <endl;
else
{
int LineNO(0); //行编号
string Line( " "); //行的信息字符串
while(getline(myfile,Line)) //读取信息
{
++LineNO;
cout < < "Line " < <LineNO < < ": "
< <Line.size() < < " Chars "
< <endl;
}
}
myfile.close();
myfile.clear();
system( "PAUSE ");
return EXIT_SUCCESS;
}