what's the difference between fprintf & printf ?
if we read directly from a file, can we use printf to write the content?
what's the difference between fprintf & printf ?
if we read directly from a file, can we use printf to write the content?
fprintf lets you select which file stream to print to. When you use printf, stdout is automatically used as the file stream.
All problems in computer science can be solved by another level of indirection,
except for the problem of too many layers of indirection.
– David J. Wheeler
sorry, I was confused...
is it correct to say that fprintf is when you want to write something to an external file?
printf is the same as fprintf(stdout, char *format, ...);
So printf puts the format string into the screen (STDOUT).
And fprintf gives you an option to output what you want into an file; fprintf(FILE *f, char *format, ...);
By the way, you can write fprintf(stdout, char *format, ...); because STDOUT (Screen) is treated as a file.
printf() function sends output to stdout and fprintf() is used to specify the output stream.
fprintf() we can use this to print into a file.
Code:printf("Hai friends\n"); //This will print output in stdout. FILE *f=fopen("test.txt","w"); fprintf(f,"%s","HELLO"); // This will print the output in test.txt.
Last edited by rekha_sri; 02-28-2010 at 10:52 PM.
printf and fprintf are same , but the difference is printf has a default stream STDOUT , that means
it can only print it in the monitor.
but fprintf can have any user defined stream , so that only we prefer fprintf to write a file.
For ex :
if you want to print the content in the STDERR , you cannot in printf , but we can do that using fprintf , since
it allows us to choose the stream. like that we can choose normal files too.
thank you for the help everyone