一道输入输出流的编程题

来源:百度知道 编辑:UC知道 时间:2024/05/21 06:44:26
1.建立一个文本文件,从键盘输入一篇短文存放在文件中。短文由若干行构成,每行不超过80个字符。
2.把第一题建立的文本文件读出,显示在屏幕上并统计该文件的行数

要用C++语言````````!!!谢谢

#include <fstream>
#include <iostream>
using namespace std;
void main(){
ofstream ofs("a.txt");
char ch[80];
while(cin>>ch)
ofs<<ch<<endl;
ofs.close;
ifstream ifs("a.txt");
int n=0;
while(ifs>>ch){
cout<<ch<<endl;
n++;
}
cout<<n<<endl;
}

//总觉得楼上的有哪里不对头
#include <fstream.h>
#include <string.h>
#include <iostream.h>
#include <stdio.h>
//#include <fstream>
//#include <iostream>
//using namespace std;
void main(){
ofstream ofs("a.txt");
char ch[80];
ifstream ifs("a.txt");
int n=0,k=1;
while(k){
strcpy(ch,"");
for(n=0;;n++){
ch[n]=getchar();
if(ch[n]=='\n'){
if(n==0)k=0;
ch[n]='\0';break;
}
if(k)ofs<<ch<<endl