開発環境
- 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-4を解いてみる。
実習14-4.
コード(TextWrangler)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(){
FILE *in_file;
FILE *out_file1;
FILE *out_file2;
char numbers[100];
int write_size,read_size;
in_file = fopen("numbers.txt","r");
if(in_file == NULL){
printf("Cannot open numbers.txt\n");
exit(8);
}
out_file1 = fopen("out1.txt","wb");
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);
}
fgets(numbers,sizeof(numbers),in_file);
write_size = fwrite(numbers,1,sizeof(numbers),out_file1);
if(write_size != sizeof(numbers)){
fprintf(stderr,"Unable to write out1.txt\n");
exit(8);
}
fclose(out_file1);
out_file1 = fopen("out1.txt","rb");
read_size = fread(numbers,1,sizeof(numbers),out_file1);
if(read_size != sizeof(numbers)){
fprintf(stderr,"Unable to read out1.txt\n");
exit (8);
}
fputs(numbers,out_file2);
fclose(in_file);
fclose(out_file1);
fclose(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
d??-d??m??-d?? ?-d?0?-d?@{-d?r??w9SW$
$ cat out2.txt
0
$
う〜ん、分からないのでとりあえず次に進んでみることに。
0 コメント:
コメントを投稿