1 // STLTest11.cpp : 定义控制台应用程序的入口点。 2 // 3 4 #include "stdafx.h" 5 #include<iostream> 6 #include<fstream> 7 #include<string> 8 9 using namespace std; 10 11 12 int _tmain(int argc, _TCHAR* argv[]) 13 { 14 15 string filename; 16 cout<<"Enter filename for new file: "; 17 cin>>filename; 18 ofstream fout(filename.c_str());//将流与文件管理 19 fout<<"For your eyes only!\n"; 20 cout<<"Enter your secet number: "; 21 float secret; 22 cin>>secret; 23 fout<<"Your secret number is "<<secret<<endl; 24 //清除文件状态标识 25 fout.clear(); 26 fout.close(); 27 28 ifstream fin(filename.c_str()); 29 cout<<"Here are the contents of "<<filename.c_str()<<": \n"; 30 char ch; 31 while(fin.get(ch)) 32 { 33 cout<<ch; 34 } 35 cout<<"Done! \n"; 36 fin.clear(); 37 fin.close(); 38 39 40 return 0; 41 }
STLTest11.cpp : 定义控制台应用程序的入口点。
原文:http://www.cnblogs.com/sunxiangguo/p/5241959.html