2012年10月27日土曜日

開発環境

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

設問14-3.

if(in_file < 0)文のブロックのfprintfの第1引数にstderrを渡していないから。

修正

コード(TextWrangler)

#include <stdio.h>
#include <stdlib.h>
#ifndef __MSDOS__
#define __UNIX__
#endif /* __MSDOS__ */

#ifdef __UNIX__
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#endif /* __UNIX__ */

#ifdef __MSDOX__
#include <fcntl.h>
#include <sys\stat.h>
#include <io.h>
#endif /* __MSDOX__ */

#ifndef O_BINARY
#define O_BINARY 0
#endif /* O_BINARY */

#define BUFFER_SIZE (16 * 1024) 

int main(int argc, char *argv[]){
  int in_file;
  in_file = open(argv[1], O_RDONLY|O_BINARY);
  if(in_file < 0){
    fprintf(stderr, "Error:Unable to open %s\n", argv[1]);
    exit(8);
  }
  close(in_file);
  return (0);
}

入出力結果(Terminal)

$ cc -g -o sample sample.c
$ ./sample abcde
Error:Unable to open abcde
$

0 コメント:

コメントを投稿