printf and write command

Go down

printf and write command

Post by Navitha on Fri Mar 12, 2010 3:31 pm

#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.

Navitha

Posts : 14
Points : 24
Join date : 2010-02-26

View user profile

Back to top Go down

Re: printf and write command

Post by Christopher on Fri Mar 12, 2010 3:49 pm

since printf buffer has not been flushed out, it is displayed just before the termination of main method.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: printf and write command

Post by Christopher on Fri Mar 12, 2010 3:53 pm

printf is executed first, but holds the data in buffer. tatsy it looks like write is executed first.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: printf and write command

Post by jeeva on Fri Mar 12, 2010 3:57 pm

can you tel how to do this in the right order!!!

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

View user profile

Back to top Go down

Re: printf and write command

Post by akalya on Fri Mar 12, 2010 4:02 pm

@jeeva
jus add an \n to end of printf statement....

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: printf and write command

Post by Sowmini on Fri Mar 12, 2010 4:05 pm

printf("You enterd"); This line has to be written as
printf("You entered\n");

Sowmini

Posts : 2
Points : 2
Join date : 2010-03-03

View user profile

Back to top Go down

Re: printf and write command

Post by jeeva on Fri Mar 12, 2010 4:06 pm

@akalya

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

View user profile

Back to top Go down

Re: printf and write command

Post by akalya on Fri Mar 12, 2010 4:21 pm

@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;
}

akalya

Posts : 70
Points : 86
Join date : 2010-03-04

View user profile

Back to top Go down

Re: printf and write command

Post by jeeva on Fri Mar 12, 2010 4:28 pm

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!!!

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

View user profile

Back to top Go down

Re: printf and write command

Post by Christopher on Fri Mar 12, 2010 4:31 pm

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.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: printf and write command

Post by Jay Dave on Mon Mar 15, 2010 12:55 pm

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)

Jay Dave

Posts : 4
Points : 4
Join date : 2010-03-03

View user profile

Back to top Go down

Re: printf and write command

Post by jeeva on Mon Mar 15, 2010 1:28 pm

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!!!

jeeva

Posts : 50
Points : 93
Join date : 2010-03-04

View user profile

Back to top Go down

Re: printf and write command

Post by Christopher on Mon Mar 15, 2010 1:32 pm

yes exactly. taty system calls are faster than library functions.
avatar
Christopher
Admin

Posts : 240
Points : 429
Join date : 2010-02-26
Age : 30

View user profile http://unixcpp.forumotion.com

Back to top Go down

Re: printf and write command

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

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