Re: Bug with where()?
- Posted by Juergen Luethje <jluethje at gmx.de> Jul 27, 2002
- 389 views
Rob <rds at RapidEuphoria.com> wrote: > Juergen writes: >> After activating the command flush(f), the program works as expected, >> but I think it also should work correctly without flush(f). > Thanks for your example. > I duplicated your results. > I think this is an example of what I say in the docs > for seek(): > "After seeking and reading (writing) a series of bytes, > you may need to call seek() explicitly before you switch > to writing (reading) bytes, even though the file position > should already be what you want." Before writing the program, I read your documentation for seek(), but now it seems to me, that I didn't understand it 100%. In my program, I used seek() immediately before puts(), but nowhere else. This lead to the known problems. Now, having read your hints, I inserted another seek() statement immediately before gets(), and now the program works correctly without flushing. Thanks! Obviously, it was not so easy for me to understand. > Explicit seeking, or flushing corrects these problems. Do these problems only occur when where() is used? > Your example ran the same (undesired) way with the DJGPP-built > interpreter as well. This seems to be a basic problem with C I/O. > It's not really under my control, except that I could do a forced, > hidden flush(), in some cases, causing a possibly severe loss of > performance. I see. > One minor thing. I recommend that you use "ub", rather than "u", > although it doesn't seem to matter in your example... > "This function [seek()] is normally used with files opened in > binary mode. In text mode, DOS [and Windows] > converts CR LF to LF on input, and LF to CR LF on output, > which can cause great confusion when you are trying to count bytes." I wanted to use gets(), because it's much easier for me, to think of a file as a "list of lines", rather than a stream of bytes. And I erroneously assumed, that gets() couldn't be used for files that are opened in binary mode. > Regards, > Rob Craig > Rapid Deployment Software > http://www.RapidEuphoria.com Thanks for your advice! Regards, Juergen