include stdio h include stdlib h int mainvoid
#include <stdio. h> #include <stdlib. h> int main(void) { FILE *fp; 例12 -1 源程序 /* 定义文件指针*/ if( ( fp = fopen("f 1. txt", "w") ) == NULL){ printf("File open error!n"); exit(0); } fprintf( fp, "%s", "Hello World! " ); /* 打开文件 */ if( fclose( fp ) ){ printf( "Can not close the file!n" ); exit(0); } /* 关闭文件 */ /* 写文件 */ return 0; } 4
自定义类型(typedef) typedef <已有类型名> <新类型名>; typedef int INTEGER; int i, j; <====> INTEGER i, j; typedef int* POINT; POINT int* p 1; <====> POINT p 1; 11
自定义类型(typedef)的使用方法 int i ¨ 变量名 新类型名 int INTEGER ¨ 加上 typedef int INTEGER ¨ 用新类型名定义变量 INTEGER i; ¨ 定义变量 int num[10] int NUM[10] typedef int NUM[10] NUM a <===> int a[10] 12
#include "stdio. h" int main(void) { FILE * fp; long num; char stname[20]; int score; if((fp = fopen("f. txt", "r")) == NULL){ printf("File open error!n"); exit(0); } 12. 2. 1 程序解析 /* 定义文件指针*/ /* 打开文件 */ while( !feof(fp) ){ fscanf(fp, "%ld%s%d", &num, stname, &score); printf("%ld %s %dn", num, stname, score); }; if( fclose(fp) ){ printf( "Can not close the file!n" ); exit(0); } } /* 关闭文件 */ 16
#include "stdio. h" int main(void) { FILE * fp; long num; char stname[20]; int score; if((fp = fopen("f. txt", "r")) == NULL){ printf("File open error!n"); exit(0); } 12. 2. 1 程序解析 /* 定义文件指针*/ /* 打开文件 */ while( !feof(fp) ){ fscanf(fp, "%ld%s%d", &num, stname, &score); printf("%ld %s %dn", num, stname, score); }; if( fclose(fp) ){ printf( "Can not close the file!n" ); exit(0); } } /* 关闭文件 */ 20
#include <stdio. h> int main(void) { FILE *fp 1, *fp 2; char c; 例12 -3 源程序 if(( fp 1 = fopen( "f 1. dat", "r" )) == NULL){ printf(" File open error!n" ); exit(0); } if(( fp 2 = fopen( "f 2. dat", "w" )) == NULL){ printf(" File open error!n" ); exit(0); } while( !feof( fp 1 ) ){ c = fgetc( fp 1 ); fputc(c, fp 2); } fclose( fp 1 ); fclose( fp 2 ); return 0; } 23
int main(void) { int i; char ch; FILE *fp; if((fp=fopen("f 2. txt", "w")) == NULL){ /* 打开文件f 2. txt */ printf("File open error!n"); exit(0); } for(i = 0; i < 10; i++){ /* 写文件 10次 */ ch = getchar(); fputc(ch, fp) ; } if(fclose(fp)){ /* 关闭文件 */ printf("Can not close the file!n" ); exit(0); } if((fp=fopen("f 2. txt", "r")) == NULL){ /* 打开文件f 2. txt */ printf("File open error!n"); exit(0); } for(i = 0; i < 10; i++){ /* 读文件 10次 */ ch = fgetc(fp); putchar(ch); } if(fclose(fp)){ /* 再次关闭文件 */ printf("Can not close the file!n"); exit(0); } return 0; } 源程序 29
将字符串"apple", "grape", "pear" 写入到磁盘文件f 12 -5. txt中,然后再从该文 件中读出,显示到屏幕。 int main(void) { FILE *fp; char a[ ][80] = {"apple", "grape", "pear"}, strout[80]=""; int i; if((fp = fopen("f 12 -5. txt", "w")) == NULL){ printf("File open error!n"); exit(0); } for(i = 0; i < 3; i++) fputs(a[i], fp); fclose(fp); if((fp = fopen("f 12 -5. txt", "r")) == NULL){ printf("File open error!n"); exit(0); } i = 0; while( !feof(fp) ){ if( fgets(strout, strlen(a[i++])+1, fp) != NULL) puts(strout); } fclose(fp); return 0; 32 } 例12 -5
cashbox. txt文件的部分内容 Log. ID Create. Date Note Charge Balance 1 2006 -06 -01 alimony 500. 00 2 2006 -06 -08 shopping -300. 00 200. 00 3 2006 -06 -15 shopping -60. 00 140. 00 4 2006 -06 -20 workingpay 200. 00 340. 00 5 2006 -08 -01 scholarship 1000. 00 1340. 00 …… 43
输出日志 void List. All. Log(FILE *cfptr) /*列出所有收支流水帐*/ { struct Log. Data log; long logcount; fseek(cfptr, 0 L, SEEK_SET); /*定位指针到文件开始位置*/ fread(&log, size, 1, cfptr); printf("logid logdate lognote charge balancen"); while(!feof(cfptr)) { printf("%6 ld %-11 s %-15 s %10. 2 lfn", logid, logdate, lognote, log. charge, log. balance); fread(&log, size, 1, cfptr); } } 45
输出日志 void List. All. Log(FILE *cfptr) /*列出所有收支流水帐*/ { struct Log. Data log; long logcount; fseek(cfptr, 0 L, SEEK_SET); /*定位指针到文件开始位置*/ printf("logid logdate lognote charge balancen"); while(!feof(cfptr)) { fread(&log, size, 1, cfptr); printf("%6 ld %-11 s %-15 s %10. 2 lfn", logid, logdate, lognote, log. charge, log. balance); } } 46
查询上次日志 void Query. Last. Log(FILE *cfptr) /*查询显示最后一条记录*/ { struct Log. Data log; long logcount; logcount=get. Logcount(cfptr); if(logcount>0){ /* 表示有记录存在 */ fseek(cfptr, size*(logcount-1), SEEK_SET); /*定位最后记录*/ fread(&log, size, 1, cfptr); /*读取最后记录*/ printf("The last log is: n"); printf("logid: %-6 ldnlogdate: %-11 snlognote: %-15 sn", logid, logdate, lognote); printf("charge: %-10. 2 lfnbalance: %-10. 2 lfn", log. charge, log. balance); } else printf("no logs in file!n"); } 47
增加日志 void Add. New. Log(FILE *cfptr) /*添加新记录*/ { struct Log. Data log, lastlog; long logcount; printf("Input logdate(format: 2006 -01 -01): "); scanf("%s", logdate); printf("Input lognote: "); scanf("%s", lognote); printf("Input Charge: Income+ and expend-: "); scanf("%lf", &log. charge); logcount=get. Logcount(cfptr); /*获取记录数*/ 48
增加日志 if(logcount>0){ fseek(cfptr, size*(logcount-1), SEEK_SET); fread(&lastlog, size, 1, cfptr); /*读入最后记录*/ logid=lastlog. logid+1; /*记录号按顺序是上次的号+1*/ log. balance=log. charge+lastlog. balance; } else { /*如果文件是初始,记录数为 0*/ logid=1; log. balance=log. charge; } fseek(cfptr, 0 L, SEEK_END); /*rewind(cfptr); */ printf("logid= %ldn", logid); fwrite(&log, sizeof(struct Log. Data), 1, cfptr); /*写入记录*/ } 49
计算日志个数 long get. Logcount(FILE *cfptr) /*获取文件记录总数*/ { long begin, end, logcount; fseek(cfptr, 0 L, SEEK_SET); begin=ftell(cfptr); fseek(cfptr, 0 L, SEEK_END); end=ftell(cfptr); logcount=(end-begin)/size; return logcount; } 50
- Slides: 51