高水準入出力関数と低水準入出力関数

高水準入出力関数

  • fopen, fread, fwrite, fclose
  • OS非依存
  • バッファリングあり

低水準入出力関数

  • open, read, write, close
  • OS 依存
  • バッファリングなし

バッファサイズ

stdio.h 内で  BUFSIZ が定義されている(らしい)

確かめてみる。

 $ grep BUFSIZ /usr/include/stdio.h

#ifndef BUFSIZ
# define BUFSIZ _IO_BUFSIZ
Else make it use buffer BUF, of size BUFSIZ. */

 では、_O_BUFSIZ はどこでいくつに定義されているのか?

 $ find /usr/include -name \*.h | xargs grep _IO_BUFSIZ

/usr/include/stdio.h:# define BUFSIZ _IO_BUFSIZ
/usr/include/x86_64-linux-gnu/bits/libio.h:#define _IO_BUFSIZ _G_BUFSIZ

 次は _G_BUFSIZ を探す。

~$ find /usr/include -name \*.h | xargs grep _G_BUFSIZ
/usr/include/x86_64-linux-gnu/bits/_G_config.h:#define _G_BUFSIZ 8192
/usr/include/x86_64-linux-gnu/bits/libio.h:#define _IO_BUFSIZ _G_BUFSIZ

 漸くたどり着いた。この処理系では 8192 byte らしい。

printf/scanf したときに 8192byte になるまではバッファリングされる、はず?

試すには、8192byte 前後の read/write し、 strace してみればよい?