問題及代碼:
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
int main()
{
fstream outfile,infile;
infile.open("abc.txt",ios::in); // (1)
if(!infile) {
cout<<"Can’t open the file."<<endl;
abort();
}
outfile.open("newabc.txt",ios::out);//(2)
if(!outfile) {
cout<<"Can’t open the file."<<endl;
abort();
}
char buf[80];
int i=1;
while(!infile.eof()) // (3)
{
infile.getline(buf,'\r'); //(4)
outfile<<i++<<": "<<buf<<endl; //(5)
}
infile.close();
outfile.close();
return 0;
}
運行結果:
學習小結:
又回去看了一下視頻,學習到一些東西
三個get函數和倆getline函數的用法,從輸入流中提取數據到字符數組或者字符變量中
不錯!
加油