c - Why is fseek or fflush always required between reading and writing in the update modes? -


q: i'm trying update file in place, using fopen mode "r+", reading string, , writing modified string, it's not working.

a: sure call fseek before write, both seek beginning of string you're trying overwrite, , because fseek or fflush required between reading , writing in read/write "+" modes.

my question why fseek or fflush required between reading , writing in read/write "+" modes? section 5.2 of andrew koenig's c traps , pitfalls (1989) mentioned because of backward compatibility issue. can explain in detail?

the library buffers input , output operations. check out setvbuf() , _iofbf, _iolbf parameters funktion. fseek() or fflush() require library commit buffered operations. standard specifies seek or flush operation mandatory allow library shortcuts; otherwise, every i/o operation, lib have check if previous operation read op (or write op), , trigger flush if "direction" of i/o changed.


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -