Wednesday, October 20, 2010

freopen() : change Mode back

Yesterday, I found a particular problem with the C function freopen(), so I decide to share it with you

Problem is with the function freopen(); the function change the role of the usual stdin, stdout for the file operation.

Problem : if I use freopen, how can I get the original stdout (or stdin) back?
FOF, this is not a good way to doing the file handling and my suggestion is always use your own explicit output (or input) stream variable, for the file operation.

Solution : solution of this particular problem is given by using dup, dup2 , dup and dup2 are the system call for duplicating underling file descriptor, which are present in UNIX/LINUX but not necessary any where else.

Reference:
http://c-faq.com/stdio/undofreopen.html
http://c-faq.com/stdio/rd.kirby.c

:-fat0ss

No comments:

Post a Comment