Library functions are based on system calls, eg:printf (library functions), and write (System calls). After redirection, the contents of fwrite were printed two times in the file because: 1. Buffer mode changed (from row buffer to full buffer) 2. The library function has a buffer, and the system call has no buffer.
Write prototype: ssize_t write (int fd, const void *buf, size_t count);
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys /types.h>int main () { pid_t id; printf ("Hello world"); const char* msg= "I love you"; write (1,msg,strlen (msg)); id=fork (); if ( id<0) { perror ("fork"); exit (1); } else if (id==0) { printf ("pid: %d, Ppid: %d ", Getpid (), Getppid ()); } else { printf ("pid: %d,ppid: %d", Getpid (), Getppid ()); } return 0;}
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/B4/wKiom1cHYvfCiq7PAAAUks-Kdsc393.png "title=" QQ picture 20160408154918.png "alt=" Wkiom1chyvfciq7paaauks-kdsc393.png "/>
REDIRECT output to file
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7E/B0/wKioL1cHZD_Ri0bCAAAdMZVpSoo684.png "title=" 10160408155156.png "alt=" Wkiol1chzd_ri0bcaaadmzvpsoo684.png "/>
Modify the above code as: int main () {pid_t id; printf ("Hello World"); Const char* msg= "I Love You"; Close (1); file* fp=fopen ("File", "w+"); Write (1,msg,strlen (msg)); Id=fork (); ...}
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M02/7E/B0/wKioL1cHZmmDCkKCAAAkoTsY-MY548.png "title=" QQ picture 20160408155943.png "alt=" Wkiol1chzmmdckkcaaakotsy-my548.png "/>
Close file descriptor: Standard output, the next file descriptor will start from the front empty small integer subscript, at which time file occupies the descriptor 1.
This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1761786
system calls and library functions under Linux