简体   繁体   中英

Write System Call

I am writing this program in LINUX

#include <fcntl.h>
int main()
{

    int rd,id;
    char buff[10]={0};

    id = open("p_child.c",O_RDONLY|O_APPEND|O_WRONLY);
    if(id>1)
    {
        printf("I am file descriptor %d \n",id);

        rd = read(id,buff,8);
        printf("I am Reading file %s",buff);

        write(1,"New\n",4);
        printf("\nI am Writing file %s \n",buff);

    }
}

Its output is like printing 'New' before READ System Call. Why is this Happening?? As Write System call is used after "printf" of Read System call.

这是因为输出一直缓冲到您写出换行符为止。

更新此行将起作用

printf("I am Reading file %s \n",buff);

You forgot to add "\\n" in printf before write system call.

rd = read(id,buff,8);
printf("I am Reading file %s\n",buff);
write(1,"New\n",4);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM