题目:建立一个文本文件,从键盘录入一篇短文存放在该文件中短文由若干行构成,每行不超过80个字符,并统计行数。
/*
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
void main(){
fstream iofs;
char *p,str[80],str1[80];
int x=0;
p=&str[1];
cout<<"Please input the file name:\n";
cin>>p;
iofs.open(p,ios::in|ios::out);
if(!iofs){ cout<<"Open iofs error!"<<endl; return ; }
while(cin>>str&&!iofs.eof()){ //sizeof()<=80
iofs<<str<<endl;
}
iofs.getline(str1,80);
while(!iofs.eof()){ x=x+1; cout<<str1<<endl; iofs.getline(str1,80); }
cout<<"总行数:"<<x<<endl;
iofs.close();
} */
//显示文件内容,并统计行数
/*
#include<iostream.h>
#include<stdlib.h>
#include<fstream.h>
void main(){
fstream iofs;
char *p,str[80],str1[80];
int x=0;
p=&str[1];
cin>>p;
iofs.open(p,ios::in);
iofs.getline(str1,80);
//do{ cout<<str1<<endl; iofs.getline(str1,80); }while(!iofs.eof());
while(!iofs.eof()){ cout<<str1<<endl; x=x+1; iofs.getline(str1,80); }
cout<<"总行数"<<x<<endl;
}*/
原文地址:https://www.cnblogs.com/WE-ON-LINE/p/11830653.html