Computer Club MNNIT
Would you like to react to this message? Create an account in a few clicks or log in to continue.

stdout vs stderr in fprintf

5 posters

Go down

stdout vs stderr in fprintf Empty stdout vs stderr in fprintf

Post  shivang 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.
shivang
shivang

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

Back to top Go down

stdout vs stderr in fprintf Empty Re: stdout vs stderr in fprintf

Post  ankurgutpa 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
ankurgutpa
ankurgutpa

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

Back to top Go down

stdout vs stderr in fprintf Empty Re: stdout vs stderr in fprintf

Post  vijay 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

Back to top Go down

stdout vs stderr in fprintf Empty Re: stdout vs stderr in fprintf

Post  Admin 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

https://computerclub09mnnit.forumotion.net

Back to top Go down

stdout vs stderr in fprintf Empty Admin

Post  ankurgutpa 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
ankurgutpa
ankurgutpa

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

Back to top Go down

stdout vs stderr in fprintf Empty Re: stdout vs stderr in fprintf

Post  Admin 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

https://computerclub09mnnit.forumotion.net

Back to top Go down

stdout vs stderr in fprintf Empty Re: stdout vs stderr in fprintf

Post  ankitgr8 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

Back to top Go down

stdout vs stderr in fprintf Empty Re: stdout vs stderr in fprintf

Post  Admin 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

https://computerclub09mnnit.forumotion.net

Back to top Go down

stdout vs stderr in fprintf Empty 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