The operating system has to 「fix up」 your I/O by ensuring that everything occurs on block-aligned boundaries and rounding up to the next largest block安全
1 #include <stdio.h> 2 3 int main(int argc, char* argv[]) 4 { 5 struct private { 6 char name[100]; /* real name */ 7 unsigned long booty; /* in pounds sterling */ 8 unsigned int beard_len; /* in inches */ 9 }; 10 struct private p; 11 struct private blackbeard = {"Edward Teach", 950, 48}; 12 13 FILE* out = fopen("data", "r"); 14 if (out == NULL) { 15 fpiintf(stderr, "fopen error\n"); 16 return 1; 17 } 18 19 if (fwrite(&blackbeard, sizeof(struct private), 1, out) == 0) { 20 fprintf(stderr, "fwrite error\n"); 21 return 1; 22 } 23 24 if (fclose(out)) { 25 fprintf(stderr, "fclose error\n"); 26 return 1; 27 } 28 29 FILE* in = fopen("data", "r"); 30 if (in == NULL) { 31 fprintf(stderr, "fopen error\n"); 32 return 1; 33 } 34 if (fread(&p, sizeof(struct private), 1, in) == 0) { 35 fprintf(stderr, "fread error\n"); 36 return 1; 37 } 38 39 if (fclose(in)) { 40 fprintf(stderr, "fclose error\n"); 41 return 1; 42 } 43 44 fprintf(stdout, "name = \"%s\" booty = %lu beard_len = %u\n", p.name, p.booty, p.beard_len); 45 return 0; 46 }
int fileno (FILE *stream); //返回文件流(C標準I/O庫)對應的文件描述符(Unix系統調用)
void flockfile (FILE *stream); void funlockfile (FILE *stream);
reading data: kernel ==> standard I/O buffer ==> application buffer
writing data: application data ==> standard I/O buffer ==> kernel