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!\n"; // 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 << ":\n"; char ch; while (fin.get(ch)) // read character from file and cout << ch; // write it to screen cout << "Done\n"; fin.close(); return 0; }
2、