c++文件操作 demo
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main(){
//从文件file.txt中读出数据(包含空格信息)
ifstream in("file.txt",ifstream::app);
if(!in){
cout << "open error" << endl;
exit(1);
}
char result1;
while(in.get(result1))
cout << result1;
cout << endl;
//////////////////////////
//写信息到file.txt文件(不含空格)
ofstream out("file.txt",ofstream::app);
if(!out){
cout << "open error" << endl;
exit(1);
}
char result[10];
int i = 0;
while(i<10){
cin>>result[i];
out.put(result[i]);
cout << result[i];
i++;
}
cout << endl;
out.close();
return 0;
}