这个程序就是新建一个文件,输入文件的内容,然后又打印出来到屏幕上。
// fileio.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <iostream>
#include <fstream>
#include <string>
int main()
{
// std::cout << "Hello World!\n";
using namespace std;
string filename;
cout << "Enter your name for new file: ";
cin >> filename;
ofstream fout(filename.c_str());
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 inpust stream object for new file and call it fin
ifstream fin(filename.c_str());
cout << "Here are the content of " << filename << ":\n";
char ch;
while (fin.get(ch))
{
cout << ch;
}
cout << "Done\n";
fin.close();
return 0;
}