stdout vs stderr in fprintf

Go down

stdout vs stderr in fprintf

Post  shivang on Sat Aug 30, 2008 1:42 pm

The following program doesn't "seem" to print "hello-out". (Try executing it)

#include <stdio.h>
#include <unistd.h>
int main()
{
while(1)
{
fprintf(stdout,"hello-out");
fprintf(stderr,"hello-err");
sleep(1);
}
return 0;
}

What could be the reason?
I don't know the answer.
avatar
shivang

Posts : 22
Join date : 2008-08-11
Age : 29
Location : Tondon 174

View user profile

Back to top Go down

Re: stdout vs stderr in fprintf

Post  ankurgutpa on Tue Sep 02, 2008 8:09 am

stderr is related to parent shell itself.
stdout related to the child shell in which process is executing and storing the o/p in buffer.This will be shown on parent shell If the program terminates or when buffer overflows. cheers
avatar
ankurgutpa

Posts : 44
Join date : 2008-08-10
Age : 30
Location : Tandon 72

View user profile

Back to top Go down

Re: stdout vs stderr in fprintf

Post  vijay on Tue Sep 02, 2008 4:28 pm

writing to stdout is writing to descriptor 1....
writing to stderr is writing to descriptor 2....

but the above strange behaviour wasn't observed with this code....
Code:

 main()
{
 while( 1 )
 {
    write(1,"out",3) ;
    write(2,"err",3) ;
    sleep(1) ;
 }
 return 0 ;
}

this still fascinates me... Smile

vijay

Posts : 6
Join date : 2008-08-18

View user profile

Back to top Go down

Re: stdout vs stderr in fprintf

Post  Admin on Fri Sep 05, 2008 10:53 pm

ankur the explanation you have given is incomplete actually it has nothing to do with parent shell and child shell this program can be changed to give proper output by just only changing one character .
vijay wen you are using write it does so because write is lower implementation of fprintf and it does not use bufferring were as fprintf does.....
Very Happy

Admin
Admin

Posts : 39
Join date : 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

Admin

Post  ankurgutpa on Sat Sep 06, 2008 4:22 am

i've read i somewhere on internet that stderr is parent shell and the process executes in child subshell and return it's o/p to parent shell farao
avatar
ankurgutpa

Posts : 44
Join date : 2008-08-10
Age : 30
Location : Tandon 72

View user profile

Back to top Go down

Re: stdout vs stderr in fprintf

Post  Admin on Sat Sep 06, 2008 6:39 am

watever you are saying is correct but that is how shell executes the command
this code can be changed as i mentioned in prev reply this is all about bufferring nothing else.......
just check it out ....... Very Happy

Admin
Admin

Posts : 39
Join date : 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

Re: stdout vs stderr in fprintf

Post  ankitgr8 on Sun Sep 07, 2008 9:25 am

actually stdout is a buffered stream whereas stderr isn't
Try running the code for 2-3 min you will get the answer

ankitgr8

Posts : 6
Join date : 2008-08-11

View user profile

Back to top Go down

Re: stdout vs stderr in fprintf

Post  Admin on Mon Sep 08, 2008 3:40 am

yes ankit your xplanation is correct .........
just try fprintf(stdout,"hello-out\n");
and observe the output .......... Very Happy

Admin
Admin

Posts : 39
Join date : 2008-08-03

View user profile http://computerclub09mnnit.forumotion.net

Back to top Go down

Re: stdout vs stderr in fprintf

Post  Sponsored content


Sponsored content


Back to top Go down

Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum