2012年10月29日月曜日

開発環境

『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の14章(ファイルの入出力)14.10(プログラミング実習/O)実習14-2を解いてみる。

実習14-2.

コード(TextWrangler)

sample.c

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

int main(){
  FILE *in_file;
  FILE *out_file;
  int ch;
  char name[100];
  printf("コピー元のファイル名を入力: ");
  fgets(name,sizeof(name),stdin);
  name[strlen(name) - 1] = '\0';
  in_file = fopen(name, "r");
  if(in_file == NULL){
    fprintf(stderr, "Cannot open %s\n", name);
    exit(8);
  }
  printf("コピー先のファイル名を入力: ");
  fgets(name, sizeof(name), stdin);
  name[strlen(name) - 1] = '\0';
  out_file = fopen(name, "w");
  if(out_file == NULL){
    fprintf(stderr, "Cannot open %s\n", name);
    exit(8);
  }
  while(1){
    ch = fgetc(in_file);
    if(ch == EOF) break;
    if(ch == '\t'){
      fputs("  ", out_file);
    } else {
      fputc(ch, out_file);
    }
  }
  return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
コピー元のファイル名を入力: tmp.txt
コピー先のファイル名を入力: tmp_out.txt
$ cat tmp.txt
ab cde fghij
klmnopqrst
$ cat tmp_out.txt
ab cde  fghij
klmnopqrst
$

タブが複数のスペースに展開されていることを確認できた!

0 コメント:

コメントを投稿