Introduce old redir program
[lcapit-junk-code.git] / books / apue / buf-prov.c
blob8548c64be42877933f85f4d8432a59dea4357732
1 /*
2 * This program proves the behaivor of buffered, line-buffered
3 * and unbufferd I/O. Read the comments to understand it.
4 */
6 #include <stdio.h>
7 #include <unistd.h>
8 #include <stdlib.h>
10 int main(void)
12 char c;
13 FILE *file;
16 * stdout is line-buffered, it means the that data is flushed
17 * when a new-line char is found. So, you should see the line
18 * bellow.
20 fprintf(stdout, "stdout is line-buffered\n");
22 /* ... and you shouldn't see the next line */
23 fprintf(stdout, "--- The hidden line ---");
25 /* stderr is unbuffered, you will see the line */
26 fprintf(stderr, "this is stderr in action");
29 * opens a file and write-data in it. When we call pause()
30 * you'll see that nothing was written to the file.
32 file = fopen("./buf_test.txt", "w+");
33 if (file) {
34 fprintf(file, "File contents\nFully buffered\n");
35 } else {
36 perror("fopen()");
37 exit(1);
40 read(0, &c, 1);
41 return 0;