www.gusucode.com > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序 > 《C++高级语言程序设计》PPT及全书例子源代码-源码程序/code/C++例题程序/第2章/s2_18/smain2_18.cpp
//Download by http://www.NewXing.com //主文件:smain2_18.cpp #include <iostream> //向程序中加入包含标准输入输出流类的头文件iostream #include <fstream> //向程序中加入包含文件输入输出流类的头文件fstream using namespace std; //使用std名字空间 void main( void ) { /* 利用ofstream类的构造函数创建一个文件输出流对象 */ ofstream fout( "d:\\mytest.txt" ); if ( ! fout) { cout << "文件不能打开" <<endl; } else { fout << "Learning C++ is very useful."<< endl; // 输出到磁盘文件 fout.close(); // 关闭文件输出流 /* 利用ifstream类的构造函数创建一个文件输入流对象 */ ifstream fin( "d:\\mytest.txt" ); if ( ! fin) { cout << "文件不能打开" <<endl; } else { char buffer[80]; fin >> buffer; // 从磁盘文件输入 fin.close(); // 关闭文件输入流 cout << buffer << endl; } } }