这是在网上看到的两个程序,已经不知道原作者是谁了,可能是流传太广了吧 嘿嘿
//Example : 从in.txt 文件中读入数据,并输出到out.txt中,输出的文件中每行之间有一空行相隔
------------------------------------方法一-----------------------------
#include <fstream>
#include <string>
#include <iostream>
using namespace std;
int main (int){
string s;
ifstream inf;
inf.open("in.txt");
//打开输出文件
ofstream outf;
outf.open("out.txt");
//从in.txt 文件中读入数据,并输出到out.txt中
/*其中 getline(参1,参2); 作用是从 inf 指向的文件中
每次读入一行,把数据存到字符串s 中,从第一行开始
每读完一行后,系统自动地把指针指向下一行,不用人为
干预*/
while( getline(inf,s ) ){
outf << s << "/n/n"; //我这里并没有用到字符串
cout << s << endl << endl; //数组,而是只用了一个串
}