fstream的簡單用法

#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!"。

 


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章