64,639
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
int main() {
string str;
ifstream fin("test.txt"/*, ios::binary*/);
if (fin.fail()) {
cout<<"can not open file"<<endl;
return 0;
}
int n=0;
if(fin.peek()==EOF)
{
cout<<"文件是空的"<<endl;
return 0;
}
//对非空文件进行统计函数
while(fin.peek()!=EOF) //如果不空
{
if(fin>>str)
{
cout<<str<<endl;
}
else
{
cout<<" "<<endl;
}
n++;
}
cout<<"行数:"<<n<<endl;
return 0;
}