// テキストファイルの入出力 // getline( )関数を使用して1行単位の入力を体験 #include <iostream> #include <fstream> using namespace std; int main( int argc, char *argv[] ) { if ( argc != 2 ) { cout << "usage: stream01 input_file" << endl; return 1; } ifstream in( argv[1] ); // 入力ストリーム if ( !in ) { cout << "can't open file" << endl; return 1; } ofstream out( "output.txt" );// 出力ストリーム if ( !out ) { cout << "can't open file" << endl; return 1; } const streamsize ssize = 1024; char cBuff[ssize]; while ( in.getline( cBuff, ssize ) ) { out << cBuff << endl; } // クローズ in.close( ); out.close( ); return 0; } /* 本ソースでgetline( ) は 1023の文字を読み込みか、改行を検出するか あるいはEOFに到達するまで、cBuff に読込みます。 改行は省いて、cBuff に格納されます。これがいやならば get( ) を使い ます。 getline( ) は入力ストリームの参照を返すので、ファイルエンドなら in.getline( ) が false になります。 */ // end of file