您的位置:首页 > 编程语言 > C语言/C++

C/C++中读写文件

2016-06-13 04:37 441 查看
头文件加上

#include <fstream>


下面是例子:

char buffer[12],buffer2[12];
// 读文件
ifstream trainingfile ("trainingData.txt");
if(!trainingfile){
cout << "Unable to open trainingfile";
exit(1); // terminate with error
}
while (!trainingfile.eof())
{
trainingfile.getline(buffer,12);
sscanf(buffer,"%c %c %c %c %c %c",&a[0],&a[1],&a[2],&a[3],&a[4],&a[5])
}
trainingfile.close();

//写文件
ofstream outputfile("output.txt");
if(!outputfile){
cout << "Unable to open otfile";
exit(1); // terminate with error
}
// 利用outputfile一直向里面添加东西即可
outputfile<<b[0]<<' '<<b[1]<<' '<<b[2]<<' '<<endl;
//写完后
outputfile.close();
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: