Introduce pet-projects dir
[lcapit-junk-code.git] / books / apue / setvbuf.c
bloba1763f017bfa6a1f0c7613493ea8382fa2d24cd8
1 /*
2 * setvbuf() usage example
3 */
4 #include <stdio.h>
5 #include <string.h>
6 #include <stdlib.h>
8 int main(void)
10 int err;
11 FILE *file;
12 char buf[BUFSIZ];
14 /* open the stream */
15 file = fopen("./buf_test2.txt", "w+");
16 if (!file) {
17 fprintf(stderr, "file cannot be openned\n");
18 exit(1);
21 /* set the stream buffer and the it as fully-buffered */
22 memset(buf, '\0', sizeof(buf));
23 err = setvbuf(file, buf, _IOFBF, sizeof(buf));
24 if (err) {
25 fprintf(stderr, "sevbuf() failed\n");
26 exit(1);
29 /* write data and exit */
30 fprintf(file, "Go, go! Goooooo Jhonny go, go! Goooooo Jhonny go, go!\n");
31 fclose(file);
33 /* shows the contents of 'buf' */
34 fprintf(stdout, "buf: %s", buf);
36 return 0;