Questions tagged [freopen]

freopen reopens a stream with a different file or mode.

Reuses stream to either open the file specified by filename or to change its access mode.

Reference: freopen

105 questions
2
votes
1 answer

freopen not writing to the specified file

I am trying to redirect output of stdout and stderr using a file. I am using freopen and it creates the file in the correct directory but the file is blank. When I comment out the code to redirect the stdout and stderr - the output shows up on the…
user1185853
  • 49
  • 2
  • 7
2
votes
1 answer

Why does reopening stdout to a newly-allocated console occasionally fail?

Consider this program: #include #include #pragma comment(linker, "/SUBSYSTEM:CONSOLE") int main(void) { if (!FreeConsole()) { MessageBoxA(NULL, "FreeConsole failed", NULL, MB_ICONHAND); return 1; } …
2
votes
1 answer

Having child processes printf to redirected stdout correctly

I am redirecting stdout on a process with freopen(), and as long as it's just one process, everything's fine. However, if I do something like this: freopen("stdout.txt", "a+", stdout); printf("Initial line.\n"); int i=0; while(i<1000) { …
Jim
  • 63
  • 5
2
votes
0 answers

when stdout is redirected to a file fflush is not enough

I have a C program with 9 calls to printf and I need it to run on a remote server for several days. So I decided to redirect the stdout to a file to check the progresses: FILE *foutput=freopen("output.txt","w",stdout); After some debugging I found…
GRquanti
  • 527
  • 8
  • 23
2
votes
2 answers

How to write in stdout after using freopen

After freopen-ing stdout, How can I print on terminal? freopen("out", "w", stdout); // reopen stdout /* something */ printf("Now I want to print this on terminal");
cijianzy
  • 71
  • 2
  • 8
2
votes
1 answer

Fortran equivalent of freopen

In C I can use the standard function freopen with stdout to redirect the standard output of my program to a file. I would like to do the same with fortran, but after having googled I couldn't find a similar feature. Does it exist? In case somebody…
Spiros
  • 2,156
  • 2
  • 23
  • 42
2
votes
1 answer

Should "freopen'ed" stdout be closed?

In a C or C++ program I can use freopen to redirect the output (i.e. the stdout file descriptor) to another file (or to discard it be reopening the file descriptor to /dev/null or another sink). The question is: if I do so, should I take care of…
Spiros
  • 2,156
  • 2
  • 23
  • 42
2
votes
1 answer

freopen or freopen_s, what exactly are they doing?

errno_t freopen_s ( FILE** pFile, const char *path, const char *mode, FILE *stream ); Here, the freopen_s disassociates the FILE pointer stream from whatever it is pointing at, then associates it with the file that is located at path. The mode…
Utkan Gezer
  • 3,009
  • 2
  • 16
  • 29
2
votes
3 answers

freopen again but still can't read after fseek

I have something as below in c++. I want to read a file for several times, but failed. Though I use fseek to move to the head of the file after I freopen again, but I still can only read it once. For the second time, cin get nothing @.@. I'm really…
2
votes
1 answer

Check the return value of freopen() in C

I'm aware that the right use of freopen is to omit the assignment, given this post: freopen("/dev/tty","r", stdin); My question is, should I still check the return value? I'm reopening stdin and closing whatever it was. For…
user1024718
  • 573
  • 6
  • 18
2
votes
1 answer

freopen_s on stdout causes problems with GetConsoleScreenBufferInfo on Windows

To temporarily redirect stdout to a file, I'm doing: printf("Before"); freopen_s(&stream, "test.txt", "w", stdout); printf("During"); freopen_s(&stream, "CONOUT$", "w", stdout); printf("After"); That works, however doing: CONSOLE_SCREEN_BUFFER_INFO…
Sydius
  • 13,567
  • 17
  • 59
  • 76
2
votes
2 answers

reassign a file pointer in loop

I need to reassign a single file pointer to multiple files sequentially. I have the file paths correctly in a string path. when i pass the path and the file pointer to a function to reassign, I get "Aborted (core dumped)".. FILE * fptr; …
tabs_over_spaces
  • 352
  • 1
  • 3
  • 14
2
votes
1 answer

Does it make sense to try and reopen a tty in binary mode?

I have stumbled on this C code. It uses freopen to reopen stdin in binary mode: if (!isatty(STDIN_FILENO)) freopen(NULL, "rb", stdin); I don't understand where the isatty test comes from. Does it mean that it does not make sense to reopen a…
epsicot
  • 121
  • 3
2
votes
1 answer

sending output stream to char[] in c

I have been using dup and freopen to rerout stdout to a file as below: fflush(stdout); fgetpos(stdout, &pos); fd = dup(fileno(stdout)); freopen("stdout.out", "w", stdout); What I would like to do would be rerout it to a char[], so that I can…
ewok
  • 20,148
  • 51
  • 149
  • 254
2
votes
1 answer

Mixing freopen and ncurses

I'm having issues in mixing freopen of stdin together with ncurses library. What I do is to reopen the stdin to a file, parse it through flex+bison, then I should revert it back and start ncurses. Code for the swap is the following: void…
Jack
  • 131,802
  • 30
  • 241
  • 343