2012年11月1日木曜日

開発環境

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

実習14-5.

コード(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];
  char name1[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);
  }
  strcpy(name1, name);
  strcat(name1, ".out");
  out_file = fopen(name1, "w");
  if(out_file == NULL){
    fprintf(stderr, "Cannot open %s\n", name1);
    exit(8);
  }
  while(1){
    ch = fgetc(in_file);
    if(ch == EOF) break;
    if(( ch & 0x80) !=0) continue;
    fputc(ch, out_file);
  }
  fclose(in_file);
  fclose(out_file);
  return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample
コピー元のファイル名: tmp.txt
$ cat tmp.txt
abcdefghijklmnopqrstuvwxwz
あいうえお
かきくけこ
1234567890
!@#$%^&*()_+
$ cat tmp.txt.out
abcdefghijklmnopqrstuvwxwz


1234567890
!@#$%^&*()_+
$

最高ビット(0x80)以上の日本語は削除されていることを確認できた。

0 コメント:

コメントを投稿