I have been reading APUE recently when a basic problem turned up. My code is shown below
#include <apue.h>
#define BUFFSIZE 4096
int main()
{
int n;
char buf[BUFFSIZE];
while((n = read(STDIN_FILENO, buf, BUFFSIZE)) > 0)
{
printf("n is %d\n", n); //this line is added by me for testing
if(write(STDOUT_FILENO, buf, n) != n)
err_sys("write error"); //functions defined by the book to print error message
}
if(n < 0)
err_sys("read error");
exit(0);
}
After compiling, when I run the program as shown below
> $ ./mycat
123456[enter]
n is 7
123456
1234[enter]
n is 5
1234
It seems that it works according to the structure of my code. And I don't quite understand the function of the 'enter'. Every time I pressed 'enter', read function terminates and pass the characters including the '\n' produced by the 'enter' to the write function. So it goes inside the loop and firstly prints the number of characters read.
However, the following testing seems go against the above and against the structure of the code.
> $ ./mycat > data
123456[enter]
1234[enter]
^D
> $ cat data
123456
1234
n is 7
n is 5
It seems like that the program firstly writes all the characters to the file then prints the value of 'n', however according to my understanding, it should firstly prints as follows
n is 7
123456
n is 5
1234
I think again and again and just couldn't figure it out. Could you please help me?