開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- C (プログラミング言語)
- Clang (コンパイラ)
C実践プログラミング 第3版 (Steve Oualline (著)、 望月 康司 (監訳) (翻訳)、谷口 功 (翻訳)、オライリー・ジャパン)のⅡ部(単純なプログラミング)の14章(ファイル入出力)、14.1(ファイル関数)、14.2(変換ルーチン)、14.3(バイナリファイルとASCIIファイル)、14.4(行終端にまつわる謎)、14.5(バイナリI/O)、14.6(バッファリングの問題)、14.7(バッファリングを行わないI/O)、設問 14-3を解いてみる。
その他参考書籍
- プログラミング言語C 第2版 ANSI規格準拠 (B.W. カーニハン D.M. リッチー (著)、 石田 晴久 (翻訳)、共立出版)
- プログラミング言語Cアンサー・ブック 第2版 (クロビス・L.トンド、スコット・E.ギンペル(著)、矢吹 道郎(翻訳))
設問14-3.
コード
sample.c
#include <stdio.h>
#ifndef __MSDOS__
#define __UNIX__
#endif /* __MSDOS__ */
#include <stdlib.h>
#ifdef __UNIX__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif /* __UNIX__ */
#ifdef __MSDOS__
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#endif /* __MSDOS__ */
#ifndef O_BINARY
#define O_BINARY 0
#endif /* O_BINARY */
#define BUFFER_SIZE (16 * 1024)
int main(int argc, char *argv[])
{
char buffer[BUFFER_SIZE];
int in_file;
int out_file;
int read_size;
if (argc != 3){
fprintf(stderr, "Error: Wrong number of arguments\n");
fprintf(stderr, "Usage is: copy <from> <to>\n");
exit (8);
}
in_file = open(argv[1], O_RDONLY|O_BINARY);
if (in_file < 0){
/* 問題ではfprintfの第1引数がstderrではなく文字列になっているので
* エラーメッセージを出力するのではなく、コアダンプが行われる
*/
fprintf(stderr, "Error: Unable to open %s\n", argv[1]);
exit (8);
}
out_file = open(argv[2], O_WRONLY|O_TRUNC|O_CREAT|O_BINARY, 0666);
if (out_file < 0){
/* 上記と同様の問題 */
fprintf(stderr, "Error: Unable to open %s\n", argv[2]);
exit (8);
}
while (1){
read_size = read(in_file, buffer, sizeof(buffer));
if (read_size == 0){
break;
}
if (read_size < 0){
fprintf(stderr, "Error: Read error\n");
exit (8);
}
write(out_file, buffer, (unsigned int) read_size);
}
close(in_file);
close(out_file);
return (0);
}
makefile
CC=cc CFLAGS=-g sample: sample.c $(CC) $(CFLAGS) -o sample sample.c clean: rm -f sample
入出力結果(Terminal)
$ make
cc -g -o sample sample.c
$ ./sample
Error: Wrong number of arguments
Usage is: copy <from> <to>
$ ./sample sample.c sample.c.bak
$ cat sample.c.bak
#include <stdio.h>
#ifndef __MSDOS__
#define __UNIX__
#endif /* __MSDOS__ */
#include <stdlib.h>
#ifdef __UNIX__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif /* __UNIX__ */
#ifdef __MSDOS__
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#endif /* __MSDOS__ */
#ifndef O_BINARY
#define O_BINARY 0
#endif /* O_BINARY */
#define BUFFER_SIZE (16 * 1024)
int main(int argc, char *argv[])
{
char buffer[BUFFER_SIZE];
int in_file;
int out_file;
int read_size;
if (argc != 3){
fprintf(stderr, "Error: Wrong number of arguments\n");
fprintf(stderr, "Usage is: copy <from> <to>\n");
exit (8);
}
in_file = open(argv[1], O_RDONLY|O_BINARY);
if (in_file < 0){
/* 問題ではfprintfの第1引数がstderrではなく文字列になっているので
* エラーメッセージを出力するのではなく、コアダンプが行われる
*/
fprintf(stderr, "Error: Unable to open %s\n", argv[1]);
exit (8);
}
out_file = open(argv[2], O_WRONLY|O_TRUNC|O_CREAT|O_BINARY, 0666);
if (out_file < 0){
/* 上記と同様の問題 */
fprintf(stderr, "Error: Unable to open %s\n", argv[2]);
exit (8);
}
while (1){
read_size = read(in_file, buffer, sizeof(buffer));
if (read_size == 0){
break;
}
if (read_size < 0){
fprintf(stderr, "Error: Read error\n");
exit (8);
}
write(out_file, buffer, (unsigned int) read_size);
}
close(in_file);
close(out_file);
return (0);
}
$ ./sample abcde temp.bak
Error: Unable to open abcde
$
0 コメント:
コメントを投稿