C

Similar documents
C/C++ - 文件IO

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

epub 33-8

新版 明解C言語入門編

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

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

C

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

C/C++ - 函数

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

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

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

C 1

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

C C

文件

C/C++ 语言 - 循环

CC213

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

nooog

第7章 数组

chap12.ppt

untitled

附錄C.doc

C

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

Microsoft Word - chap13.doc

Microsoft PowerPoint - C-Ch12.ppt

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

C/C++ - 数组与指针

untitled

untitled

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

untitled

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

C++ 程式設計

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

Ps22Pdf

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

PowerPoint 演示文稿

Microsoft Word - CPE考生使用手冊 docx

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


untitled

IO

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

CC213

Ps22Pdf

CC213

Microsoft PowerPoint - 20-string-s.pptx

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)

11. 下列关于文件的结论中正确的是 ( ) A. 对文件操作必须先关闭文件 B. 对文件操作必须先打开文件 C. 对文件的操作顺序没有统一规定 D. 以上三种答案全是错误的 12. 若以 a+ 方式打开一个已存在的文件, 则以下叙述正确的是 ( ) A. 文件打开时, 原有文件内容不被删除, 位置

<4D F736F F D20B5DA31D5C220C7B6C8EBCABDD3A6D3C3B3CCD0F2C9E8BCC6D7DBBACFBDCCB3CC2E646F63>

FY.DOC

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

第3章 类型、运算符与表达式

华恒家庭网关方案

static struct file_operations gpio_ctl_fops={ ioctl: gpio_ctl_ioctl, open : gpio_open, release: gpio_release, ; #defineled1_on() (GPBDAT &= ~0x1) #def

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

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("%

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

C/C++ Basics

新版 明解C++入門編

C/C++ Programming

untitled

1. SDK 概述 Arcsoft Photo Styling SDK 可以将输入图像转化为具有指定风格的图像 图像风格主要是指色调 笔触 线条等图像的特异性表现形式 存储某一艺术图像风格模板的文件将按需要发布给 SDK 使用者 借助 SDK 和风格模板文件, 用户可以开发图像风格化等图像美化类应用

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

1

3.1 num = 3 ch = 'C' 2

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

Generated by Unregistered Batch DOC TO PDF Converter , please register! 浙江大学 C 程序设计及实验 试题卷 学年春季学期考试时间 : 2003 年 6 月 20 日上午 8:3

第3章.doc

液晶之家论坛-

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

02

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

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

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

untitled

第三章

nb.PDF

bnbqw.PDF

南華大學數位論文

Microsoft Word 一年級散文教案.doc

米食天地教案

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

1. 本文首段的主要作用是 A. 指出 異蛇 的藥用功效 說明 永之人爭奔走焉 的原因 B. 突出 異蛇 的毒性 為下文 幾死者數矣 作鋪墊 C. 交代以蛇賦稅的背景 引起下文蔣氏有關捕蛇的敘述 2. 本文首段從三方面突出蛇的 異 下列哪一項不屬其中之一 A. 顏色之異 B. 動作之異 C. 毒性之

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,

海淀区、房山区(四)

Transcription:

C 14 2017 5 31

1. 2. 3. 4. 5. 2/101 C

1.

( ) 4/101 C

C ASCII ASCII ASCII 5/101 C

10000 00100111 00010000 ASCII 10000 31H 30H 30H 30H 30H 1 0 0 0 0 0 ASCII 6/101 C

7/101 C

( ) ( ) 8/101 C

UNIX ANSI C 9/101 C

FILE 10/101 C

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; 11/101 C

FILE * FILE * fp; 12/101 C

2.

ANSI C fopen() 14/101 C

fopen() FILE * fopen(const char * path,const char * mode); path mode 15/101 C

FILE * fp; fp = fopen("file1", "r"); file1 file1 NULL 16/101 C

: "r" "w" "a" "rb" "wb" "ab" 17/101 C

(fopen) : "r+" / "w+" / "a+" / "rb+" / "wb+" / "ab+" / 18/101 C

"r" "r" "w" 19/101 C

"a" "r+" "w+" "a+" 20/101 C

fopen() "r" "r" "rb" "r+" "rb+" NULL FILE * fp; if ( (fp=fopen("file1", "r")) ==NULL ) { printf("cannot open this file\n"); exit(0); } 21/101 C

22/101 C

(fclose) 23/101 C

fclose() int fclose( FILE * fp ); fclose() 0 EOF(-1) 24/101 C

25/101 C

I 1 /* count.c -- using standard I/O */ 2 #include <stdio.h> 3 #include <stdlib.h> // ANSI C exit() prototype 4 5 int main(int argc, char *argv[]) 6 { 7 int ch; 8 // place to store each character as read 9 FILE *fp; 10 // "file pointer" 11 long count = 0; 12 13 if (argc!= 2) { 26/101 C

II 14 printf("usage: %s filename\n", argv[0]); 15 exit(1); 16 } 17 18 if ((fp = fopen(argv[1], "r")) == NULL) { 19 printf("can t open %s\n", argv[1]); 20 exit(1); 21 } 22 23 while ((ch = getc(fp))!= EOF) { 24 putc(ch,stdout); // same as putchar(ch); 25 count++; 26 } 27 fclose(fp); 27/101 C

III 28 printf("file %s has %ld characters\n", argv[1], count); 29 30 return 0; 31 } 28/101 C

3.

fputc() fgetc() fprintf() fscanf() fread() fwrite() 30/101 C

fputc() fputc() int fputc(int c, FILE * stream); fputc() c unsigned char stream c ASCII EOF 31/101 C

fgetc() fgetc() int fgetc(file * stream); fgetc() stream EOF. EOF feof(fp) feof(fp)==1 32/101 C

fgetc() 33/101 C

fgetc() I 1 // file2screen.c: 2 #include<stdio.h> 3 #include<stdlib.h> 4 5 int main(int argc, char * argv[]) 6 { 7 FILE * fp; 8 int ch; 9 10 if (argc!= 2) { 11 printf("usage: %s filename\n", argv[0]); 12 exit(1); 13 } 34/101 C

fgetc() II 14 15 if ((fp = fopen(argv[1], "r")) == NULL) { 16 printf("can t open %s\n", argv[1]); 17 exit(1); 18 } 19 20 while ((ch = fgetc(fp))!= EOF) { 21 putchar(ch); // same as putchar(ch); 22 } 23 24 return 0; 25 } 35/101 C

fgetc() while(!feof(fp)) { ch = fgetc(fp);... } 36/101 C

fputc() fgetc() # 37/101 C

fputc() fgetc() I 1 // screen2file.c: 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main(void) 5 { 6 char ch; 7 FILE *fp; 8 9 if ( (fp = fopen("file1.txt", "w")) == NULL) { 10 printf("cannot open file1.txt!\n"); 11 exit(1); 12 } 13 while ((ch = getchar())!= # ) 38/101 C

fputc() fgetc() II 14 fputc(ch, fp); 15 fclose(fp); 16 17 return 0; 18 } 39/101 C

fputc() fgetc() 40/101 C

fputc() fgetc() I 1 // copy.c: 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main(int argc, char * argv[]) 5 { 6 char ch; 7 FILE * fsrc, * fdes; 8 9 if (argc < 3) { 10 printf("usage: %s srcfile desfile\n", argv[0]); 11 exit(1); 12 } 13 41/101 C

fputc() fgetc() II 14 15 if ((fsrc = fopen(argv[1], "r")) == NULL 16 (fdes = fopen(argv[2], "w")) == NULL) { 17 printf("cannot open files!\n"); 18 exit(1); 19 } 20 21 while ((ch = getc(fsrc))!= EOF) 22 fputc(ch, fdes); 23 printf("successed copy srcfile to desfile\n"); 24 25 fclose(fsrc); 26 fclose(fdes); 27 42/101 C

fputc() fgetc() III 28 return 0; 29 } 43/101 C

fscanf() fprintf() fprintf() fscanf() printf() scanf() fprintf() fscanf() printf() scanf() 44/101 C

fscanf() fprintf() int fprintf(file * stream, const char *format,...); int fscanf (FILE * stream, const char *format,...); printf()/scanf() 45/101 C

fscanf() fprintf() fprintf(fp, "%d, %6.2f", i, t); i t %d %6.2f fp i=3, t=4.5 3, 4.50 46/101 C

fscanf() fprintf() fscanf() ASCII fscanf(fp, "%d, %f", &i, &t); 3, 4.5 3 i 4.5 t 47/101 C

fscanf() fprintf() 48/101 C

fscanf() fprintf() I 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAX 40 4 int main(void) 5 { 6 FILE *fp; 7 char words[max]; 8 9 if((fp = fopen("words", "a+")) == NULL) { 10 fprintf(stdout, "Can t open \"words\" file.\n"); 11 exit(1); 12 } 13 49/101 C

fscanf() fprintf() II 14 puts("enter words to add to the file: press the Enter "); 15 puts("key at the beginning of a line to terminate." ); 16 while ( gets(words)!= NULL && words[0]!= \0 ) 17 fprintf(fp, "%s", words); 18 puts("file contents:"); 19 rewind(fp); 20 while(fscanf(fp, "%s", words)==1) 21 puts(words); 22 fclose(fp); 23 24 return 0; 25 } 50/101 C

fscanf() fprintf() III 51/101 C

fscanf() fprintf() fprintf() fscanf() ASCII fprintf() fscanf() fread() fwrite() 52/101 C

putw() getw() putw() getw() ( ) putw(10, fp); /* 10 fp */ i = getw(fp); /* fp i */ 53/101 C

fgets() fputs() fgets() fgets(str, n, fp); // fp n-1 str // str \0 fputs() fputs(str, fp); // str fp 54/101 C

fread() fwrite() num 8 0.333333 double num = 1./3.; fprintf(fp,"%f", num); %.2 4 0.33 %.12f 14 0.333333333333 55/101 C

fread() fwrite() num 0.33 fprintf() 56/101 C

fread() fwrite() double double fread() fwrite() 57/101 C

fread() fwrite() fwrite() size_t fwrite(const void * ptr, size_t size, size_t nmemb, FILE * fp); ptr size nmemb fp nmemb nmemb 58/101 C

fread() fwrite() 256 char buffer[256]; fwrite(buffer, 256, 1, fp) 256 59/101 C

fread() fwrite() 10 double double arr[10]; fwrite(arr, sizeof(double), 10, fp) arr 10 double 60/101 C

fread() fwrite() fread() size_t fread(const void * ptr, size_t size, size_t nmemb, FILE * fp); ptr size nmemb fp nmemb nmemb 61/101 C

fread() fwrite() 10 double double arr[10]; fread(arr, sizeof(double), 10, fp) 10 double arr 62/101 C

fread() fwrite() struct student_type { char name[10]; int num; int age; char addr[30]; } stu[40]; stu 40 63/101 C

fread() fwrite() for fread() 40 for(i=0; i<40; i++) fread(&stu[i], sizeof(struct student_type), 1, fp); fread(stu, sizeof(struct student_type), 40, fp); 64/101 C

fread() fwrite() for(i=0; i<40; i++) fwrite(&stu[i], sizeof(struct student_type), 1, fp); fwrite(stu, sizeof(struct student_type), 40, fp); 65/101 C

fread() fwrite() 66/101 C

fread() fwrite() I 1 #include <stdio.h> 2 #include <stdlib.h> 3 #define SIZE 4 4 void save(void); 5 6 struct stu_type 7 { 8 char name[20]; 9 int num; 10 int age; 11 char addr[15]; 12 } stu[size]; 13 67/101 C

fread() fwrite() II 14 int main(void) 15 { 16 int i; 17 FILE * fp; 18 printf("sizeof(structstud)=%d\n", (int) sizeof( struct stu_type)); 19 printf("please input the 4 student information, " 20 "including name, num, age, address\n"); 21 for(i = 0; i < SIZE; i++) 22 scanf("%s%d%d%s", stu[i].name, &stu[i].num, 23 &stu[i].age, stu[i].addr); 24 save(); 25 printf("\nthe information of the 4 students is:\n") ; 68/101 C

fread() fwrite() III 26 fp = fopen("stu_list.txt","rb"); 27 28 fread(stu, sizeof(struct stu_type), SIZE, fp); 29 for(i = 0; i < SIZE; i++) 30 { 31 // fread(&stu[i], sizeof(struct stu_type), 1, fp); 32 printf("%-10s%4d%4d%15s\n", stu[i].name, stu[i]. num, 33 stu[i].age, stu[i].addr); 34 } 35 fclose(fp); 36 37 return 0; 38 } 69/101 C

fread() fwrite() IV 39 40 void save(void) 41 { 42 FILE * fp; 43 int i; 44 45 if ((fp = fopen("stu_list.txt", "wb")) == NULL) { 46 printf("cannot open file!\n"); 47 exit(1); 48 } 49 for (i = 0; i < SIZE; i++) { 50 if (fwrite(&stu[i], sizeof(struct stu_type), 1, fp)!= 1) 51 printf("file write error.\n"); 70/101 C

fread() fwrite() V 52 } 53 fclose(fp); 54 55 } 71/101 C

fread() fwrite() 72/101 C

fread() fwrite() I 1 #include <stdio.h> 2 #include <stdlib.h> 3 int main(void) 4 { 5 float buffer[] = {1.0, 2.0, 3.0}; 6 float read[3]; 7 FILE * fp; 8 9 if ((fp = fopen("file3.txt", "wb")) == NULL) { 10 printf("cannot open files.\n"); 11 exit(0); 12 } 13 fwrite(buffer, 1, sizeof(buffer), fp); 73/101 C

fread() fwrite() II 14 fclose(fp); 15 16 if ((fp = fopen("file3.txt", "rb")) == NULL) { 17 printf("cannot open files.\n"); 18 exit(0); 19 } 20 fread(read, 1, sizeof(read), fp); 21 printf("%f %f %f\n", read[0], read[1], read[2]); 22 fclose(fp); 23 24 return 0; 25 } 74/101 C

4.

ftell() rewind()/fseek() 76/101 C

rewind() void rewind(file * stream); fp 77/101 C

rewind() 78/101 C

rewind() I 1 // rewind.c: 2 #include <stdio.h> 3 #include <stdlib.h> 4 int main(int argc, char * argv[]) 5 { 6 int ch; 7 FILE * fsrc, * fdes; 8 9 if (argc < 3) { 10 printf("usage: %s srcfile desfile\n", argv[0]); 11 exit(1); 12 } 13 79/101 C

rewind() II 14 if ((fsrc = fopen(argv[1], "r")) == NULL 15 (fdes = fopen(argv[2], "w")) == NULL) { 16 printf("cannot open files!\n"); 17 exit(1); 18 } 19 while ((ch = getc(fsrc))!= EOF) 20 putchar(ch); 21 22 rewind(fsrc); 23 while ((ch = getc(fsrc))!= EOF) 24 fputc(ch, fdes); 25 26 fclose(fsrc); 27 fclose(fdes); 80/101 C

rewind() III 28 29 return 0; 30 } 81/101 C

rewind() file.txt C primer plus C programming $ gcc rewind.c -o rewind $./rewind file.txt file1.txt C primer plus C programming 82/101 C

fseek() fseek() int fseek(file * stream, long offset, int fromwhere); fseek() 0-1 83/101 C

fseek() SEEK_SET 0 SEEK_CUR 1 SEEK_END 2 84/101 C

fseek() fseek(fp, 0L, SEEK_SET); // fseek(fp, 10L, SEEK_SET); // 10 fseek(fp, 2L, SEEK_CUR); // //2 fseek(fp, 0L, SEEK_END); // fseek(fp, -10L, SEEK_END);// 10 85/101 C

ftell() ftell() ANSI C i = ftell(fp); if(i == -1L) printf("error\n"); i ( fp ) "error" 86/101 C

fseek() ftell() 87/101 C

fseek() ftell() I 1 // reverse.c 2 #include<stdio.h> 3 #include<stdlib.h> 4 #define CNTL_Z \032 5 #define SLEN 50 6 int main(void) 7 { 8 char file[slen]; 9 char ch; 10 FILE * fp; 11 long count, last; 12 88/101 C

fseek() ftell() II 13 puts("enter the name of the file to be processed: " ); 14 gets(file); 15 16 if ((fp = fopen(file, "rb"))==null) { 17 printf("reverse can t be open %s\n", file); 18 exit(1); 19 } 20 21 fseek(fp, 0L, SEEK_END); 22 last = ftell(fp); 23 24 for (count = 1L; count <= last; count++) { 25 fseek(fp, -count, SEEK_END); 89/101 C

fseek() ftell() III 26 ch = getc(fp); 27 if (ch!= CNTL_Z && ch!= \r ) 28 putchar(ch); 29 } 30 31 putchar( \n ); 32 fclose(fp); 33 34 return 0; 35 } 90/101 C

fseek() ftell() // file4 Hello World! I love WHU! Enter the name of the file to be processed: file4!uhw evol I!dlroW olleh 91/101 C

fseek() ftell() double 92/101 C

fseek() ftell() I 1 // randbin.c 2 #include <stdio.h> 3 #include <stdlib.h> 4 #define SIZE 1000 5 6 int main(void) 7 { 8 double arr[size]; 9 double value; 10 const char * file = "number.dat"; 11 int i; 12 long pos; 13 FILE * fp; 93/101 C

fseek() ftell() II 14 15 // Creat an array with double elements 16 for (i = 0; i < SIZE; i++) 17 arr[i] = 100.0 * i + 1.0 / (i + 1); 18 19 // Attempt to open file 20 if ((fp = fopen(file, "wb")) == NULL) { 21 fprintf(stderr, "Could not open %s for output", file); 22 exit(1); 23 } 24 25 // Write the data in the array into file with binary mode 94/101 C

fseek() ftell() III 26 fwrite(arr, sizeof(double), SIZE, fp); 27 28 // Close the file 29 fclose(fp); 30 31 // Attempt to open file 32 if ((fp = fopen(file, "rb")) == NULL) { 33 fprintf(stderr, "Could not open %s for random acess", file); 34 exit(1); 35 } 36 37 // Read selected item in the file 95/101 C

fseek() ftell() IV 38 printf("enter an index in the range 0-%d\n", SIZE -1); 39 scanf("%d", &i); 40 41 while(i >= 0 && i < SIZE) { 42 pos = (long) i * sizeof(double); // compute offset 43 fseek(fp, pos, SEEK_SET); 44 fread(&value, sizeof(double), 1, fp); 45 printf("the value there is %f.\n", value); 46 printf("next index (out of range to quit):\n"); 47 scanf("%d", &i); 48 } 49 fclose(fp); 96/101 C

fseek() ftell() V 50 puts("bye!"); 51 52 return 0; 53 } 97/101 C

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): 98/101 C

fseek() ftell() II -1 Bye! 99/101 C

5.

C 101/101 C

ferror() fopen() NULL ferror() int ferror(file * stream); 0 0 102/101 C

cleanerr int ferror(file * stream); 0 ferror(fp) 0 clearerr(fp) ferror(fp) 0 103/101 C