#include <fstream>
#include "iostream.h"
#include <string>
using std::string;
using std::ofstream;
using std::ifstream;
int main()
{
ofstream outfile( "c:/out_file.txt" );
ifstream infile( "c:/in_file.txt" );
if( !outfile ){
cerr << "error: unable to open output file!/n" ;
return -1;
}
if( !infile ){
cerr << "error: unable to open input file!/n" ;
return -1;
}
string word;
while ( infile >> word )
outfile <<word << ;
return 0;
}
建立文本文件in_file.txt, 並寫入“hello world!”
運行程序
打開out_file.txt
文件裏已被寫入“hello world!"。