开发者

operations on files in c++

for example we have created file in c++ how to write 开发者_开发问答content in this file and then output on screen?


Read this and then come back and ask if you have a more specific question, thanks.


Taken from here.

Writing:

#include <iostream>
#include <fstream>
using namespace std;

int main () {
    ofstream myfile;
    myfile.open ("example.txt");
    myfile << "Writing this to a file.\n";
    myfile.close();
    return 0;
}

Reading:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
    string line;
    ifstream myfile("example.txt");
    while(getline(myfile,line)) {
        cout << line << endl;
    }
    myfile.close();
    return 0;
}

You should really use Google.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜