C/C++ IO
Table of contents 1. 2. 3. 4. 1
C ASCII ASCII ASCII 2
10000 00100111 00010000 31H, 30H, 30H, 30H, 30H 1, 0, 0, 0, 0 ASCII 3
4
5
UNIX ANSI C 5
FILE FILE 6
stdio.h typedef struct { int level ; // unsigned flags ; // char fd; // unsigned char hold ; // int bsize ; // unsigned char * buffer ;// unsigned char * curp ; // unsigned istemp ; // short token ; // } FILE ; 7
FILE * fp; 8
9
ANSI C fopen() FILE * fopen ( const char * path, const char * mode ); path mode 10
FILE * fp; fp = fopen (" file1. txt ", "r"); file1.txt file1.txt NULL 11
1: "r" "w" "a" "rb" "wb" "ab" 12
(fopen) 2: "r+" / "w+" / "a+" / "rb+" / "wb+" / "ab+" / 13
"r" "w" "a" 14
"r+" / "w+" / "a+" / 15
fopen() "r" ("r", "rb", "r+", "rb+") NULL FILE * fp; if ( (fp= fopen (" file1 ", "r")) == NULL ) { printf (" cannot open this file \n"); exit (0) ; } 16
(fclose) fclose() fclose() int fclose ( FILE * fp ); int 0 EOF(-1) 17
(fclose) 18
19
i // count. c -- using standard I/ O # include <stdio.h> # include < stdlib.h > // ANSI C exit () prototype int main ( int argc, char * argv []) { int ch; FILE * fp; long count = 0; if ( argc!= 2) { printf (" Usage : %s filename \n", argv [0]) ; exit (1) ; } if (( fp = fopen ( argv [1], "r")) == NULL ) { printf (" Can t open %s\n", argv [1]) ; exit (1) ; } 20
ii } while (( ch = getc (fp))!= EOF ) { putc (ch, stdout ); // same as putchar (ch); count ++; } fclose (fp); printf ("\ nfile %s has %ld characters.\n", argv [1], count ); return 0; 21
fputc() fgetc() fputs() fgets() fprintf() fscanf() 22
fputc() int fputc ( int c, FILE * stream ); c unsigned char stream c ASCII EOF 23
fgetc() int fgetc ( FILE * stream ); stream EOF feof() 0 24
fgetc() 25
fgetc() i // file2screen. c: # include < stdio.h> # include < stdlib.h> int main ( int argc, char * argv []) { FILE * fp; int ch; if ( argc!= 2) { printf (" Usage : %s filename \n", argv [0]) ; exit (1) ; } if (( fp = fopen ( argv [1], "r")) == NULL ) { printf (" Can t open %s\n", argv [1]) ; exit (1) ; } while (( ch = fgetc (fp))!= EOF ) { 26
fgetc() ii } fputc (ch, stdout ); // same as putchar (ch); } return 0; 27
fgetc() i // fgetc1.c: # include < stdio.h> # include < stdlib.h> int main ( int argc, char * argv []) { FILE * fp; int ch; if ( argc!= 2) { printf (" Usage : %s filename \n", argv [0]) ; exit (1) ; } if (( fp = fopen ( argv [1], "r")) == NULL ) { printf (" Can t open %s\n", argv [1]) ; exit (1) ; } while (! feof (fp) ) 28
fgetc() ii } putchar ( fgetc (fp)); fclose (fp); return 0; 29
fputc() fgetc() EOF 30
fputc() fgetc() // screen2file. c: # include <stdio.h> # include <stdlib.h> int main ( void ) { char ch; FILE *fp; if ( (fp = fopen (" file1. txt ", "w")) == NULL ) { printf (" Cannot open file1. txt!\n"); exit (1) ; } while (( ch = getchar ())!= EOF ) fputc (ch, fp); fclose (fp); return 0; } 31
fputc() fgetc() 32
fputc() fgetc() i // copy.c: # include <stdio.h> # include <stdlib.h> int main ( int argc, char * argv []) { char ch; FILE * fsrc, * fdes ; if ( argc < 3) { printf (" Usage : %s srcfile desfile \n", argv [0]) ; exit (1) ; } if (( fsrc = fopen ( argv [1], "r")) == NULL ( fdes = fopen ( argv [2], "w")) == NULL ) { printf (" Cannot open files!\n"); exit (1) ; } 33
fputc() fgetc() ii } while (( ch = getc ( fsrc ))!= EOF ) fputc (ch, fdes ); printf (" Successed copy srcfile to desfile \ n"); fclose ( fsrc ); fclose ( fdes ); return 0; 34
fputs() fgets() int fputs ( const char *, FILE *); fputs (" Hello world ", fp); "Hello world" fp fputs() 0 EOF 35
fputss() fgets() char * fgets ( char *, FILE *); fgets (str, n, fp) n fp n-1 \0 n str n-1 EOF 36
fscanf() fprintf() fprintf(), fscanf() printf(), scanf() fprintf() fscanf() printf() scanf() 37
fscanf() fprintf() int fprintf ( FILE * stream, const char * format,...) ; int fscanf ( FILE * stream, const char * format,...) ; printf()/scanf() 38
fscanf() fprintf() fprintf (fp, "%d, %6.2 f", i, t); i t %d %6.2f fp i=3, t=4.5 3, 4.50 39
fscanf() fprintf() fscanf() ASCII fscanf (fp, "%d, %f", &i, &t); 3, 4.5 3 i 4.5 t 40
fscanf() fprintf() 41
fscanf() fprintf() i # include <stdio.h> # include <stdlib.h> # define MAX 40 int main ( int argc, char * argv []) { FILE * fp; char words [ MAX ]; if ( argc!= 2) { printf (" Usage : %s filename \n", argv [0]) ; exit (1) ; } if (( fp = fopen ( argv [1], "a+")) == NULL ) { fprintf ( stdout, " Can t open \" words \" file.\n"); exit (1) ; } puts (" Enter words to add to the file "); 42
fscanf() fprintf() ii } puts ("( press the Enter key at the beginning of "); puts ("a line to terminate.)"); while ( gets ( words )!= NULL && words [0]!= \0 ) fprintf (fp, "%s", words ); puts (" File contents :"); rewind (fp); while ( fscanf (fp, "%s", words ) ==1) puts ( words ); fclose (fp); return 0; 43
fscanf() fprintf() fprintf() fscanf() ASCII fprintf() fscanf() fread() fwrite() 44
fread() fwrite() num 8 0.333333 double num = 1./ 3.; fprintf (fp,"%f", num ); %.2 4 0.33 %.12f 14 0.333333333333 num 0.33 fprintf() 45
fread() fwrite() double double fread() fwrite() 46
fread() fwrite() fwrite() size_ t fwrite ( const void * ptr, size_ t size, size_ t nmemb, FILE * fp); ptr size nmemb fp nmemb nmemb 47
fread() fwrite() 256 char buffer [ 256]; fwrite ( buffer, 256, 1, fp) 256 48
fread() fwrite() 10 double double arr [10]; fwrite (arr, sizeof ( double ), 10, fp) arr 10 double 49
fread() fwrite() fread() size_ t fread ( const void * ptr, size_ t size, size_ t nmemb, FILE * fp); ptr size nmemb fp nmemb nmemb 50
fread() fwrite() 10 double double arr [10]; fread (arr, sizeof ( double ), 10, fp) 10 double arr 51
fread() fwrite() typedef struct { char name [10]; int num ; int age ; char addr [30]; } Student ; Student st [40]; st 40 52
fread() fwrite() for fread() 40 for (i =0; i <40; i ++) fread (& st[i], sizeof ( Student ), 1, fp); fread (st, sizeof ( Student ), 40, fp); 53
fread() fwrite() for (i =0; i <40; i ++) fwrite (& st[i], sizeof ( Student ), 1, fp); fwrite (st, sizeof ( Student ), 40, fp); 54
fread() fwrite() 55
fread() fwrite() i # include <stdio.h> # include <stdlib.h> int main ( void ) { float buffer [] = {1.0, 2.0, 3.0}; float array [3]; FILE * fp; if (( fp = fopen (" file3. txt ", "wb")) == NULL ) { printf (" Cannot open files.\n"); exit (0) ; } fwrite ( buffer, sizeof ( buffer ), 1, fp); fclose (fp); if (( fp = fopen (" file3. txt ", "rb")) == NULL ) { printf (" Cannot open files.\n"); exit (0) ; 56
fread() fwrite() ii } } farray ( array, sizeof ( array ), 1, fp); printf ("%f %f %f\n", array [0], array [1], array [2]) ; fclose (fp); return 0; 57
fread() fwrite() 58
fread() fwrite() i # include <stdio.h> # include <stdlib.h> # define SIZE 4 typedef struct { char name [20]; int num ; int age ; char addr [15]; } Student ; Student st[ SIZE ]; void save ( void ); int main ( void ) { int i; FILE * fp; 59
fread() fwrite() ii printf (" sizeof ( Student )=%d\n", ( int ) sizeof ( Student ) ); printf (" Please input the 4 student information, " " including name, num, age, address \n"); for (i = 0; i < SIZE ; i ++) scanf ("%s%d%d%s", st[i]. name, &st[i]. num, &st[i]. age, st[i]. addr ); save (); printf ("\ nthe information of the 4 students is :\ n"); fp = fopen (" student_list. txt ", "rb"); fread (st, sizeof ( Student ), SIZE, fp); for (i = 0; i < SIZE ; i ++) { printf ("% -10s%4d%4d%15 s\n", st[i]. name, st[i]. num, st[i]. age, st[i]. addr ); } fclose (fp); 60
fread() fwrite() iii return 0; } void save ( void ) { FILE * fp; int i; if (( fp = fopen (" student_list. txt ", "wb")) == NULL ) { printf (" Cannot open file!\n"); exit (1) ; } for (i = 0; i < SIZE ; i ++) { if ( fwrite (& st[i], sizeof ( Student ), 1, fp)!= 1) printf (" file write error.\n"); } fclose (fp); 61
fread() fwrite() iv } 62
ftell() rewind()/fseek() 63
rewind() void rewind ( FILE * stream ); fp 64
rewind() 65
rewind() i // rewind.c: # include <stdio.h> # include <stdlib.h> int main ( int argc, char * argv []) { int ch; FILE * fsrc, * fdes ; if ( argc < 3) { printf (" Usage : %s srcfile desfile \n", argv [0]) ; exit (1) ; } if (( fsrc = fopen ( argv [1], "r")) == NULL ( fdes = fopen ( argv [2], "w")) == NULL ) { printf (" Cannot open files!\n"); exit (1) ; } 66
rewind() ii } while (( ch = getc ( fsrc ))!= EOF ) putchar (ch); rewind ( fsrc ); while (( ch = getc ( fsrc ))!= EOF ) fputc (ch, fdes ); fclose ( fsrc ); fclose ( fdes ); return 0; 67
rewind() file.txt C primer plus C programming $ gcc rewind. c - o rewind $./ rewind file. txt file1. txt C primer plus C programming 68
fseek() fseek() int fseek ( FILE * stream, long offset, int fromwhere ); fromwhere offset fseek() 0-1 69
fseek() SEEK_SET 0 SEEK_CUR 1 SEEK_END 2 70
fseek() // fseek (fp, 0L, SEEK_SET ); // 10 fseek (fp, 10L, SEEK_SET ); // 2 fseek (fp, 2L, SEEK_CUR ); // fseek (fp, 0L, SEEK_END ); // 10 fseek (fp, -10L, SEEK_END ); 71
ftell() long int ftell ( FILE * stream ); stream -1L i = ftell (fp); if(i == -1L) printf (" error \n"); i ( fp ) "error" 72
fseek() ftell() 73
fseek() ftell() i // reverse.c # include < stdio.h> # include < stdlib.h> # define CNTL_Z \032 # define SLEN 50 int main ( void ) { char file [ SLEN ]; char ch; FILE * fp; long count, last ; puts (" Enter the name of the file to be processed : ") ; gets ( file ); if (( fp = fopen (file, "rb"))== NULL ) { printf (" reverse can t be open %s\n", file ); 74
fseek() ftell() ii } exit (1) ; } fseek (fp, 0L, SEEK_END ); last = ftell (fp); for ( count = 1L; count <= last ; count ++) { fseek (fp, -count, SEEK_END ); ch = getc (fp); if (ch!= CNTL_Z && ch!= \r ) putchar (ch); } putchar ( \n ); fclose (fp); return 0; 75
fseek() ftell() // file4 Hello World! I love WHU! Enter the name of the file to be processed : file4! UHW evol I! dlrow olleh 76
fseek() ftell() double 77
fseek() ftell() i // randbin.c # include <stdio.h> # include <stdlib.h> # define SIZE 1000 int main ( void ) { double arr [ SIZE ]; double value ; const char * file = " number. dat "; int i; long pos ; FILE * fp; // Creat an array with double elements for ( i = 0; i < SIZE ; i ++) arr [i] = 100.0 * i + 1.0 / (i + 1); // Attempt to open file 78
fseek() ftell() ii if (( fp = fopen (file, "wb")) == NULL ) { fprintf ( stderr, " Could not open % s for output ", file ); exit (1) ; } // Write the data in the array into file with binary mode fwrite (arr, sizeof ( double ), SIZE, fp); // Close the file fclose (fp); // Attempt to open file if (( fp = fopen (file, "rb")) == NULL ) { fprintf ( stderr, " Could not open % s for random acess ", file ); exit (1) ; } 79
fseek() ftell() iii } // Read selected item in the file printf (" Enter an index in the range 0 -% d\ n", SIZE -1) ; while ( ( scanf ("%d", &i) == 1) && (i >= 0 && i < SIZE ) ) { pos = ( long ) i * sizeof ( double ); // compute offset fseek (fp, pos, SEEK_SET ); fread (& value, sizeof ( double ), 1, fp); printf (" The value there is %f.\n", value ); printf (" Next index ( out of range to quit ):\n"); } fclose (fp); puts (" Bye!"); return 0; 80
fseek() ftell() i Enter an index in the range 0-999 1 The value there is 100. 500000. Next index ( out of range to quit ): 4 The value there is 400. 200000. Next index ( out of range to quit ): 5 The value there is 500. 166667. Next index ( out of range to quit ): 100 The value there is 10000. 009901. Next index ( out of range to quit ): -1 Bye! 81