after dup2, stream still contains old contents?

Posted by BobTurbo on Stack Overflow See other posts from Stack Overflow or by BobTurbo
Published on 2010-05-02T12:23:40Z Indexed on 2010/05/02 12:27 UTC
Read the original article Hit count: 156

Filed under:
|
|
|
|

so if I do:

dup2(0, backup); // backup stdin
dup2(somefile, 0); // somefile has four lines of content
fgets(...stdin); // consume one line
fgets(....stdin); // consume two lines
dup2(backup, 0); // switch stdin back to keyboard

I am finding at this point.. stdin still contains the two lines I haven't consumed. Why is that? Because there is just one buffer no matter how many times you redirect? How do I get rid of the two lines left but still remember where I was in the somefile stream when I want to go back to it?

© Stack Overflow or respective owner

Related posts about unix

Related posts about dup2