csci4061/notes/06-files-dirs-code/mixed-std-low.c

25 lines
564 B
C
Raw Permalink Normal View History

2018-01-29 23:28:37 +00:00
// Program demonstrating strange effects of mixing C std and Unix
// system I/O. Typical output is
//
// FIRST: 1
// NEXT: 41 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
//
// which is explainable with internal buffering of C std I/O
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]){
FILE *input = fopen("3K.txt","r");
int first;
fscanf(input, "%d", &first);
printf("FIRST: %d\n",first);
int fd = fileno(input);
char *buf[64];
read(fd, buf, 63);
buf[127] = '\0';
printf("NEXT: %s\n",buf);
return 0;
}