C/C++ - 文件IO

Similar documents
C

プログラムの設計と実現II

新版 明解C言語入門編

新・解きながら学ぶC言語

新・明解C言語入門編『索引』

epub 33-8

C 1

C/C++ - 函数

_汪_文前新ok[3.1].doc

C/C++ - 字符串与字符串函数

C/C++程序设计 - 字符串与格式化输入/输出

C/C++ - 字符输入输出和字符确认

C/C++ 语言 - 循环

C/C++语言 - C/C++数据

CC213

Microsoft Word - chap13.doc

C

文件

( CIP) /. :, ( ) ISBN TP CIP ( 2005) : : : : * : : 174 ( A ) : : ( 023) : ( 023)

,,,,,,,,,, ( http: \ \ www. ncre. cn,, ) 30,,,,,,,, C : C : : 19 : : : /16 : : 96 : : : ISBN 7

nooog

C/C++语言 - 运算符、表达式和语句

Microsoft Word - 把时间当作朋友(2011第3版)3.0.b.06.doc

C/C++ - 数组与指针

untitled

C C

C/C++语言 - 分支结构

untitled


Microsoft Word - CPE考生使用手冊 docx

Microsoft PowerPoint - 20-string-s.pptx

C++ 程式設計

Ps22Pdf

2013 C 1 # include <stdio.h> 2 int main ( void ) 3 { 4 int cases, a, b, i; 5 scanf ("%d", & cases ); 6 for (i = 0;i < cases ;i ++) 7 { 8 scanf ("%d %d

IO

C

untitled

1 Project New Project 1 2 Windows 1 3 N C test Windows uv2 KEIL uvision2 1 2 New Project Ateml AT89C AT89C51 3 KEIL Demo C C File

Ps22Pdf

C/C++ - 结构体、共用体、枚举体

C 1 # include <stdio.h> 2 int main ( void ) { 4 int cases, i; 5 long long a, b; 6 scanf ("%d", & cases ); 7 for (i = 0;i < cases ;i ++) 8 { 9

FY.DOC

CC213

chap12.ppt

华恒家庭网关方案

第7章 数组

附錄C.doc

int *p int a 0x00C7 0x00C7 0x00C int I[2], *pi = &I[0]; pi++; char C[2], *pc = &C[0]; pc++; float F[2], *pf = &F[0]; pf++;

Microsoft Word - 考试大纲 (2)

CC213

untitled

untitled

上海市教育考试院关于印发新修订的

3.1 num = 3 ch = 'C' 2

51 C 51 isp 10 C PCB C C C C KEIL

2015年计算机二级(C语言)模拟试题及答案(四)

[改訂新版]C言語による標準アルゴリズム事典

实际问题 : 1 职工信息管理系统 2 学生选课系统 3 飞机订票系统 4 图书信息管理系统 5 图象处理

新版 明解C++入門編

c_cpp

untitled

02

C C C The Most Beautiful Language and Most Dangerous Language in the Programming World! C 2 C C C 4 C Project 30 C Project 3 60 Project 40

第11章 可调内核参数


6 C51 ANSI C Turbo C C51 Turbo C C51 C51 C51 C51 C51 C51 C51 C51 C C C51 C51 ANSI C MCS-51 C51 ANSI C C C51 bit Byte bit sbit

untitled

1 LINUX IDE Emacs gcc gdb Emacs + gcc + gdb IDE Emacs IDE C Emacs Emacs IDE ICE Integrated Computing Environment Emacs Unix Linux Emacs Emacs Emacs Un

2013 C 1 #include <stdio.h> 2 int main(void) 3 { 4 int cases, i; 5 long long a, b; 6 scanf("%d", &cases); 7 for (i = 0; i < cases; i++) 8 { 9 scanf("%

全国计算机技术与软件专业技术资格(水平)考试

綜合社會保障援助指引

untitled

項 訴 求 在 考 慮 到 整 體 的 財 政 承 擔 以 及 資 源 分 配 的 公 平 性 下, 政 府 採 取 了 較 簡 單 直 接 的 一 次 性 減 稅 和 增 加 免 稅 額 方 式, 以 回 應 中 產 家 庭 的 不 同 訴 求 ( 三 ) 取 消 外 傭 徵 費 6. 行 政 長

(f) (g) (h) (ii) (iii) (a) (b) (c) (d) 208

南華大學數位論文

Microsoft Word 一年級散文教案.doc

米食天地教案

第32回独立行政法人評価委員会日本貿易保険部会 資料1-1 平成22年度財務諸表等

Microsoft Word - 08 单元一儿童文学理论

untitled

bnbqw.PDF

第三章

nb.PDF

Microsoft Word - 發布版---規範_全文_.doc

概 述 随 着 中 国 高 等 教 育 数 量 扩 张 目 标 的 逐 步 实 现, 提 高 教 育 质 量 的 重 要 性 日 益 凸 显 发 布 高 校 毕 业 生 就 业 质 量 年 度 报 告, 是 高 等 学 校 建 立 健 全 就 业 状 况 反 馈 机 制 引 导 高 校 优 化 招

鱼类丰产养殖技术(二).doc

疾病诊治实务(一)

名人养生.doc

<4D F736F F D2040B9C5B871A661B0CFABC8AE61C2A7AB55ACE3A8735FA7F5ABD8BFB3B9C5B871A661B0CFABC8AE61C2A7AB55ACE3A8732E646F63>


中老年保健必读(十).doc

27 i

% % ,542 12,336 14,53 16,165 18,934 22,698 25, ,557 7,48 8,877 11, 13,732 17,283 22,

海淀区、房山区(四)

穨ecr1_c.PDF

穨2005_-c.PDF

北京理工大学.doc

尲㐵.⸮⸮⸮⸮⸮

东城区(下)

果树高产栽培技术(一).doc

Transcription:

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