ファイルから1文字ずつ読み込む/書き込む


#include <stdio.h>
#include <stdlib.h>

void main( int argc, char *argv[] )
{
 FILE *fpIn, *fpOut;
 int c;

 // 引数チェック
 if ( argc != 3 ) {
  printf( "引数の数が違います\n" );
  exit( 1 );
 }

 // ファイルオープン
 if ( ( fpIn = fopen( argv[1], "r" ) ) == NULL ) {
  printf( "入力ファイルがオープンできません\n" );
  exit( 1 );
 }
 if ( ( fpOut = fopen( argv[2], "w" ) ) == NULL ) {
  printf( "出力ファイルがオープンできません\n" );
  exit( 1 );
 }

 // メイン処理
 while ( ( c = getc( fpIn ) ) != EOF )
  fputc( c, fpOut );

 // ファイルクローズ
 fclose( fpIn );
 fclose( fpOut );
}

C > 標準関数 > ファイル入出力関数 | comments (0) | trackbacks (0)

Comments

Comment Form

icons:

Trackbacks