開発環境
- OS X Lion - Apple(OS)
- TextWrangler(Text Editor) (BBEditの無料、light版)
- 言語: C
- コンパイラ: UNIX ccコンパイラ (汎用UNIX)
『実践プログラミング 第3版』 (Steve Oualline (著)、 望月 康司 (監修) (翻訳)、 谷口 功 (翻訳)、 オライリー・ジャパン、1998年、ISBN978-4900900646) II部(単純なプログラミング)の14章(ファイルの入出力)14.10(プログラミング実習/O)実習14-4を解いてみる。
実習14-4.
コード(TextWrangler)
sample.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *in_file;
FILE *out_file;
char line[100];
char *string_ptr;
in_file = fopen("tmp.txt", "r");
if(in_file == NULL){
fprintf(stderr, "Cannot open %s\n", "tmp.txt");
exit(8);
}
out_file = fopen("tmp1.txt", "wb");
if(out_file == NULL){
fprintf(stderr, "Cannot open %s\n", "tmp1.txt");
exit(8);
}
while(1){
string_ptr = fgets(line, sizeof(line), in_file);
if((int) string_ptr == EOF || string_ptr == NULL) break;
fputs(line, out_file);
}
fclose(in_file);
fclose(out_file);
return (0);
}
sample1.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *in_file;
FILE *out_file;
char line[100];
char *string_ptr;
in_file = fopen("tmp1.txt", "rb");
if(in_file == NULL){
fprintf(stderr, "Cannot open %s\n", "tmp1.txt");
exit(8);
}
out_file = fopen("tmp2.txt", "w");
if(out_file == NULL){
fprintf(stderr, "Cannot open %s\n", "tmp2.txt");
exit(8);
}
while(1){
string_ptr = fgets(line, sizeof(line), in_file);
if((int) string_ptr == EOF || string_ptr == NULL) break;
fputs(line, out_file);
}
fclose(in_file);
fclose(out_file);
return (0);
}
入出力結果(Terminal)
$ cc -g -o sample sample.c $ cc -g -o sample1 sample1.c $ ./sample $ ./sample1 $ cat tmp.txt 0 1 2 3 4 5 6 7 8 9$ cat tmp1.txt 0 1 2 3 4 5 6 7 8 9$ cat tmp2.txt 0 1 2 3 4 5 6 7 8 9$
Windowsで確認してないけど、こんな感じでいいのかな。。
0 コメント:
コメントを投稿