Created
January 16, 2016 09:39
-
-
Save arya-oss/e6bffbefa455fe554c4b to your computer and use it in GitHub Desktop.
input and output redirection in unix system in c language. very helpful in Networks Programming
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Author: Rajmani Arya | |
* Originally By: http://stackoverflow.com/users/121747/jack | |
*/ | |
static int fd_in, fd_out; | |
static fpos_t pos_in, pos_out; | |
void switchStdin(const char *newStream) | |
{ | |
fflush(stdin); | |
fgetpos(stdin, &pos_in); | |
fd_in = dup(fileno(stdin)); | |
freopen(newStream, "r", stdin); | |
} | |
void revertStdin() | |
{ | |
fflush(stdin); | |
dup2(fd_in, fileno(stdin)); | |
close(fd_in); | |
clearerr(stdin); | |
fsetpos(stdin, &pos_in); | |
} | |
void switchStdout(const char *newStream) | |
{ | |
fflush(stdout); | |
fgetpos(stdout, &pos_out); | |
fd_out = dup(fileno(stdout)); | |
freopen(newStream, "w", stdout); | |
} | |
void revertStdout() | |
{ | |
fflush(stdout); | |
dup2(fd_out, fileno(stdout)); | |
close(fd_out); | |
clearerr(stdout); | |
fsetpos(stdout, &pos_out); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment