ファイルを追記モードで開くとseekできない、という話 (C言語の高水準I/OとかPerlとか)

C言語に初めて触ってから20年のロートルですが今まで知りませんでした。man fopen より。

The fopen() function opens the file whose name is the string pointed to by filename and associates a
stream with it.

``a''   Open for writing.  The file is created if it does not exist.  The stream is positioned at the
        end of the file.  Subsequent writes to the file will always end up at the then current end of
        file, irrespective of any intervening fseek(3) or similar.

``a+''  Open for reading and writing.  The file is created if it does not exist.  The stream is posi-
        tioned at the end of the file.  Subsequent writes to the file will always end up at the then
        current end of file, irrespective of any intervening fseek(3) or similar.

いやらしいのが、fseek は成功を返すのに、データを書く位置は必ずファイルの末尾になる、と。なんでそんな仕様になってるんだろう? Perl の標準関数 open (の >> モード) も同様。

やっぱり低水準I/Oが好き♡と思った土曜の夜でしたorz