C/C++中读写文件

来源:互联网 发布:origin软件使用教程 编辑:程序博客网 时间:2024/06/07 23:50

头文件加上

#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();
0 0
原创粉丝点击