Reputation: 1212
From what I researched, freopen() in C consists of a third parameter which is a stream pointer.
so suppose I use the code:
freopen("data.txt","w",stdout);
printf("hello world\");
Would the current stream "stdout" now be swapped for the data.txt descriptor or stream? So if I now done the printf line...hello world goes to data.txt?
Just getting a bit confused.
Upvotes: 0
Views: 102
Reputation: 3764
Look at the example here:
#include <stdio.h>
int main ()
{
FILE *fp;
printf("This text is redirected to stdout\n");
fp = freopen("file.txt", "w+", stdout);
printf("This text is redirected to file.txt\n");
fclose(fp);
return(0);
}
In this example, the first line is printed to stdout, but the second line gets directed to file.txt because of freopen. freopen redirects output from the third parameter's filestream to the new filestream of the file in the first parameter.
You can look at the description of the function in the C reference here: http://www.tutorialspoint.com/c_standard_library/c_function_freopen.htm
Using this resource would have saved you the time of asking the question as your code is identical to the example.
Upvotes: 1
Reputation: 51
yes, "hello world" will goes to data.txt but be careful, you are missing an 'n' at the end of printf
Upvotes: 0