Re: How it works?



"Mike Wahler" <mkwahler@xxxxxxxxxxxx> writes:
"Mohan" <mohan.smvec@xxxxxxxxx> wrote in message
news:1146353770.445575.238950@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Hi Friends,
The following code is working fine and printing the string "Mohan S".

#include<stdio.h>
int main()
{
FILE *fp;
fp=stdout;
fprintf(fp,"%s","Mohan S");
}

My doubt is how it is possible to assign a standard output stream to a
file stream....

Because they have the same type. 'stdout' has
type 'FILE *'. 'stdout', 'stdin', and 'stderr'
*are* FILE* streams.

Why it didn't throw any error;

Why should it? What error do you expect?

instead its running
fine...

It should, except for one thing: The last character output
should be a '\n', or 'fflush()' should be called, or the
output isn't guaranteed to appear.

Even with fflush(), it's implementation-defined whether a stream (such
as stdout) is required to end in a newline.

It's very likely that the output will appear (though on many systems
it may be immediately overwritten), but it's not guaranteed. Just add
a '\n', and you'll be fine.

--
Keith Thompson (The_Other_Keith) kst-u@xxxxxxx <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
.



Relevant Pages

  • Re: safe alternative to va_arg
    ... Using that, you can easily redirect a stream to stdout, a file or ... int main{ ...
    (microsoft.public.vc.language)
  • Re: pipedstreams
    ... time readInt returns values that has never been written to the stream ... to put the wrapped streams into your Comm object. ... catch (IOException e){System.err.println("An error occured ... public synchronized void send(int dest, ...
    (comp.lang.java.programmer)
  • Re: Sending XML data via socket
    ... I looked int a link you have provided. ... // Get a stream object for reading and writing ... Resize the buffer, put in the byte we've just ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: adapting getline
    ... int getline; ... The function reads characters from the input stream stream and stores them ...
    (comp.lang.c)
  • Re: how to remove code duplication
    ... the log directory should containing the same files as the out ... exit(EXIT_FAILURE); ... int main ... void process_log_data(FILE *stream) ...
    (comp.lang.c)