| Joe Wreschnig ( @ 2006-06-02 23:58:00 |
Today's lesson: r+b
I consider myself a pretty adept C hacker. I was surprised to learn this:
When a file is opened in update mode ('+' as the third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output may not directly be followed by input without an intervening call to ... a file positioning function, and input cannot be directly followed by output without an intervening call to a file-positioning function (fseek), unless the input routine encounters an end-of-file.
Knowing this would've saved me from spending two full days tracking down a bug in Mutagen the hard way - from high-level Ogg processing down to the C library, and undoubtedly prevented several headaches for Michael and myself.
I don't have a copy of the ANSI standard to give a real cite for the above, so here's what Stevens says:
At some point I must've read that and promptly forgotten it. I can't find it mentioned in any man pages. The code itself only had mysterious breakage on AMD64 systems (and only for some seek and write values!), and ran on x86 just fine.
I consider myself a pretty adept C hacker. I was surprised to learn this:
When a file is opened in update mode ('+' as the third character in the above list of mode argument values), both input and output may be performed on the associated stream. However, output may not directly be followed by input without an intervening call to ... a file positioning function, and input cannot be directly followed by output without an intervening call to a file-positioning function (fseek), unless the input routine encounters an end-of-file.
Knowing this would've saved me from spending two full days tracking down a bug in Mutagen the hard way - from high-level Ogg processing down to the C library, and undoubtedly prevented several headaches for Michael and myself.
I don't have a copy of the ANSI standard to give a real cite for the above, so here's what Stevens says:
- Output cannot be directly followed by input without an intervening fflush, fseek, fsetpos, or rewind.
- Input cannot be directly followed by output without an intervening fseek, fsetpos, or rewind, or an input operation that encounters an end of file.
At some point I must've read that and promptly forgotten it. I can't find it mentioned in any man pages. The code itself only had mysterious breakage on AMD64 systems (and only for some seek and write values!), and ran on x86 just fine.