printf and write command
+2
Christopher
Navitha
6 posters
printf and write command
#include<stdio.h>
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, char* argv[])
{
int desc=0,count=500;
char* buf;
int rd = read(desc, buf, 500);
printf("You enterd");
write(desc, buf, rd);
sync();
close(desc);
}
[335937@oracleclient ~]$ vi seek.c
[335937@oracleclient ~]$ gcc seek.c
[335937@oracleclient ~]$ ./a.out
This is for test
This is for test
You enterd[335937@oracleclient ~]$
why write command is executed first. can any one explain.
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<unistd.h>
int main(int argc, char* argv[])
{
int desc=0,count=500;
char* buf;
int rd = read(desc, buf, 500);
printf("You enterd");
write(desc, buf, rd);
sync();
close(desc);
}
[335937@oracleclient ~]$ vi seek.c
[335937@oracleclient ~]$ gcc seek.c
[335937@oracleclient ~]$ ./a.out
This is for test
This is for test
You enterd[335937@oracleclient ~]$
why write command is executed first. can any one explain.
Navitha- Posts : 14
Points : 24
Join date : 2010-02-26
Re: printf and write command
since printf buffer has not been flushed out, it is displayed just before the termination of main method.
Re: printf and write command
printf is executed first, but holds the data in buffer. tatsy it looks like write is executed first.
Re: printf and write command
can you tel how to do this in the right order!!!
jeeva- Posts : 50
Points : 93
Join date : 2010-03-04
Re: printf and write command
@jeeva
jus add an \n to end of printf statement....
jus add an \n to end of printf statement....
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: printf and write command
printf("You enterd"); This line has to be written as
printf("You entered\n");
printf("You entered\n");
Sowmini- Posts : 2
Points : 2
Join date : 2010-03-03
Re: printf and write command
@akalya
is your answer same as christo answer!!! or does it mean somethin else!!
i mean... is this somethin related to flushin stuff!!!
is your answer same as christo answer!!! or does it mean somethin else!!
i mean... is this somethin related to flushin stuff!!!
jeeva- Posts : 50
Points : 93
Join date : 2010-03-04
Re: printf and write command
@jeeva
try this
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc, char* argv[])
{
int desc=0,count=500;
char* buf;
int rd = read(desc, buf, 500);
printf("You enterd");
fflush(stdout);
write(desc, buf, rd);
close(desc);
return 0;
}
try this
#include<sys/stat.h>
#include<sys/types.h>
#include<fcntl.h>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
int main(int argc, char* argv[])
{
int desc=0,count=500;
char* buf;
int rd = read(desc, buf, 500);
printf("You enterd");
fflush(stdout);
write(desc, buf, rd);
close(desc);
return 0;
}
akalya- Posts : 70
Points : 86
Join date : 2010-03-04
Re: printf and write command
thankyou akalya
and i also got the meaning of \n from the other thread
and does this mean that fflush function is mandatory after all printf to print the statement in that same position!!!
and i also got the meaning of \n from the other thread
and does this mean that fflush function is mandatory after all printf to print the statement in that same position!!!
jeeva- Posts : 50
Points : 93
Join date : 2010-03-04
Re: printf and write command
yes it is. if u don want to print a new line but u want to display the contents of buffer immediately, u have to go for fflush.
Re: printf and write command
printf is a library function and so it stores its output in a buffer...all library functions inturn call system functions (kernel or OS function) example: fopen is a library function that calls open (system call) to perform its task...
when there is a system call (in our example it is write, it being an OS function is executed faster than the library call...though it is not always necessary...it depends on the size of data that the OS function has to handle)
when there is a system call (in our example it is write, it being an OS function is executed faster than the library call...though it is not always necessary...it depends on the size of data that the OS function has to handle)
Jay Dave- Posts : 4
Points : 4
Join date : 2010-03-03
Re: printf and write command
so does this mean that
the lib fn calls the system call n gets the task done ...where the system call returns the output to the calling library fn which inturn stores the obtained output in the buffer... which must be explicitely taken out (n thats why display fn is not done immediately in case of printf)
whereas the system calls directly give the output (n thats why write displays things immediately..)
is this right!!!
the lib fn calls the system call n gets the task done ...where the system call returns the output to the calling library fn which inturn stores the obtained output in the buffer... which must be explicitely taken out (n thats why display fn is not done immediately in case of printf)
whereas the system calls directly give the output (n thats why write displays things immediately..)
is this right!!!
jeeva- Posts : 50
Points : 93
Join date : 2010-03-04
Similar topics
» unix command!!!!
» Cut command - problems
» CDUP COMMAND
» Command error
» how to use a variable in sed command
» Cut command - problems
» CDUP COMMAND
» Command error
» how to use a variable in sed command
Permissions in this forum:
You cannot reply to topics in this forum