开发者

operations on files in c++

开发者 https://www.devze.com 2023-01-09 18:13 出处:网络
for example we havecreated 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

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

精彩评论

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