C++读取文件

1、自主命名文件名字,从终端输入一些文字然后又从文件读取出来到终端;

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    using namespace std;
    // creat your file name
    string filename;

    cout << "Enter name for new file: ";
    cin >> filename;

    // create output stream object for new file and call it fout
    ofstream fout("jarr.txt");

    fout << "For your eyes only!
";        // write to file
    cout << "Enter your secret number: ";   // write to screen
    float secret;
    cin >> secret;
    fout << "Your secret number is " << secret << endl;
    fout.close();           // close file

    // create input stream object for new file and call it fin
    ifstream fin("jar.txt");
    //cout << "Here are the contents of " << filename << ":
";
    char ch;
    while (fin.get(ch))     // read character from file and
        cout << ch;         // write it to screen
    cout << "Done
";
    fin.close();
    return 0;
}

2、

原文地址:https://www.cnblogs.com/darklights/p/10768485.html