開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料、light版)
- 言語: C
- コンパイラ: UNIX ccコンパイラ (汎用UNIX)
『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の14章(ファイル入出力)14.10(プログラミング実習)実習14-3を解いてみる。
実習14-3.
コード(TextWrangler)
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(){ FILE *in_file; FILE *out_file1; FILE *out_file2; char ch; int num; in_file = fopen("numbers.txt","r"); if(in_file == NULL){ printf("Cannot open numbers.txt\n"); exit(8); } out_file1 = fopen("out1.txt","w"); if(out_file1 == NULL){ printf("Cannot open out1.txt\n"); exit(8); } out_file2 = fopen("out2.txt","w"); if(out_file2 == NULL){ printf("Cannot open out2.txt\n"); exit(8); } while(1){ ch = fgetc(in_file); if(ch == EOF){ fputc('\n',out_file1); fputc('\n',out_file2); break; } if(ch == '\n'){ continue; } num = ch - '0'; if(num % 3 == 0){ fputc(ch,out_file1); fputc(' ',out_file1); } else { fputc(ch,out_file2); fputc(' ',out_file2); } } return (0); }
入出力結果(Terminal)
$ cc -g -o sample sample.c $ ./sample $ cat numbers.txt 0 1 2 3 4 5 6 7 8 9 $ cat out1.txt 0 3 6 9 $ cat out2.txt 1 2 4 5 7 8 $
0 コメント:
コメントを投稿