C语言程序设计(王立柱)第七章答案 文件

函数汇总:

文件操作的首操作和尾操作:

FILE* fopen(char* filename, char* mode);//打开文件并把这个文件与一个FILE结构指针联系起来

//mode是打开文件的方式,见书本P181,文件未成功打开返回指针0值

int fclose(FILE* fp);//使用文件完毕,释放文件缓冲区,关闭文件

//执行写操作后关闭文件,会将文件缓冲区的剩余数据写入文件,不关闭文件会丢失这部分数据

//成功关闭文件返回0值,否则返回非0值

字符的读写:

int fputc(int c,FILE* fp);//将字符c写入fp指向的当前位置,成功返回1,不成功返回EOF(-1)

int fgetc(FILE* fp);//从fp的当前位置读取一个字符作为返回值

//每次执行读,写操作后指针会自动后移一个字节

字符串读写:

int fputs(char* s,FILE* fp);//舍去串结束符'\0'后写入文件fp指向的当前位置

//成功返回非负数,否则返回EOF(-1)

int fgets(char* s,int n,FILE* fp);//从fp当前位置最多读取n-1个字符,末尾加'\0'

//读取正常结束返回s,否则返回指针0

格式读写:

fscanf(stdin,scanf的内容);//等价于scanf();

fprintf(stdout,printf的内容);//等价于printf();

//把键盘文件指针stdin和显示器文件指针stdout换成磁盘文件指针,就是磁盘文件的格式读写函数

无格式读写:

int fwrite(void* buffer, int size, int n, FILE* fp);//从程序数据地址区buffer(比如创建的一个结构的地址)开始,连续size个字节作为一个数据段,一个n个数据段写入文件输出缓冲区,返回值为实际写入的数据段数量

int fread(void* buffer, int size, int n, FILE* fp);//将文件输入缓冲区中连续size个字节作为一个数据段,一共n个数据段写入buffer指向的程序数据区,返回值为实际读取的数据段数

花絮:

exit(1);

islower();//是小写字母返回1

toupper();//小写字母转换为大写

feof();//若数据指针到文件尾,返回非0值,否则返回0

atol(),atof();//如:把学号(long)和成绩(double)作为字符串读取后转化为long和double

fflush(stdin);//清空缓冲区

程序练习:

1

#include<stdio.h>
#include<stdlib.h>

int main() {
	char s[10];
	FILE* readfile;
	FILE* writefile;
	readfile = fopen("D:\\one.txt", "r");
	if (!readfile) {
		printf("cannot open file one.txt");
		exit(1);
	}
	writefile = fopen("D:\\copy.txt", "w+");
	if (!readfile) {
		printf("cannot open file copy.txt");
		exit(1);
	}
	while (!feof(readfile)) {
		fgets(s, 10, readfile);
		fputs(s, writefile);
	}
	fclose(readfile);
	fclose(writefile);
	return 0;
}

这个东西她有时候会把最后一段数字多复制一遍,比如说

kajfhaudhfuo
56465165
奥克兰商家
123 123 123    452    452
sakjaskc
45164683
卡萨斯看
月          yue           5452

最后的5452隔几个空格再复制一遍,规律是以回车结尾的文件复制是没有问题的,但是结尾不加回车就导致多复制了一段内容。查了一下代码是没问题,文件确实应该以回车结尾,但是为什么我还不清楚

2,3,4,5题

2015001        70    70    140
2015003        60    85    145
2015005        95    80    175
2015007        85    80    165
2015010        75    85    160

首先liberal文件是要事先建好的,不然就要额外多一个步骤专门写liberal文件

然后,我原本试图偷懒,在main里面FILE*并初始化五个文件指针再传递指针到每一题对应的函数操作,最后再main里面fclose五个文件。显然我失败了,原因在于首先fopen的方式是r,w,r+,w+都需要考虑很容易出错,然后最重要的是因为在对文件的每次读写后,文件指针都会自动后移,所以除了第一个函数成功后面的都失败了,表现为文件是一片空白。

总之对文件指针的定义和初始化和关闭都写在一个函数里面,不同的操作要分开,连续进行不同的操作,文件指针自动后移就会出错,比如fscanf什么都读取不到

#include<stdio.h>
#include<stdlib.h>
typedef struct {
	unsigned long id;
	int math;
	int programming;
	int total;
}Science;

void t2() {
	FILE* science;
	science = fopen("D:\\science.txt", "w");
	if (!science) {
		printf("cannot open file science.txt");
		exit(1);
	}
	Science a[5];
	int i;
	fprintf(stdout, "输入5个学生的学号和三项成绩:\n");
	for (i = 0; i < 5; i++)
		fscanf(stdin, "%ld%d%d%d", &a[i].id, &a[i].math,
			&a[i].programming, &a[i].total);
	for (i = 0; i < 5; i++)
		fprintf(science, "%ld\t\t%d\t%d\t%d\n", a[i].id, a[i].math,
			a[i].programming, a[i].total);
	fclose(science);
}
void t3() {
	FILE* science;
	FILE* science_sel;
	science = fopen("D:\\science.txt", "r");
	if (!science) {
		printf("cannot open file science.txt");
		exit(1);
	}
	science_sel = fopen("D:\\science_sel.txt", "w");
	if (!science_sel) {
		printf("cannot open file science_sel.txt");
		exit(1);
	}
	Science a;
	int i;
	for (i = 0; i < 5; i++) {
		fscanf(science, "%ld%d%d%d", &a.id, &a.math,
			&a.programming, &a.total);
		if (a.total > 150)
			fprintf(science_sel, "%ld\t\t%d\t%d\t%d\n", a.id, a.math,
				a.programming, a.total);
	}
	fclose(science);
	fclose(science_sel);
}
void t4() {
	FILE* science;
	FILE* science_pro;
	science = fopen("D:\\science.txt", "r");
	if (!science) {
		printf("cannot open file science.txt");
		exit(1);
	}
	science_pro = fopen("D:\\science_pro.txt", "w");
	if (!science_pro) {
		printf("cannot open file science_pro.txt");
		exit(1);
	}
	Science a;
	int i;
	for (i = 0; i < 5; i++) {
		fscanf(science, "%ld%d%d%d", &a.id, &a.math,
			&a.programming, &a.total);
		fprintf(science_pro, "%ld\t\t%d\n", a.id, a.total);
	}
	fclose(science);
	fclose(science_pro);
}
void t5() {
	FILE* science;
	science = fopen("D:\\science.txt", "r");
	if (!science) {
		printf("cannot open file science.txt");
		exit(1);
	}
	FILE* liberal;
	liberal = fopen("D:\\liberal.txt", "r");
	if (!liberal) {
		printf("cannot open file liberal.txt");
		exit(1);
	}
	FILE* science_liberal;
	science_liberal = fopen("D:\\science_liberal.txt", "w");
	if (!liberal) {
		printf("cannot open file science_liberal.txt");
		exit(1);
	}

	Science a[2];
	int i;

	for (i = 0; i < 5; i++) {
		fscanf(science, "%ld%d%d%d", &a[0].id, &a[0].math,
			&a[0].programming, &a[0].total);//读science取一组成绩放到a[0]
		fscanf(liberal, "%ld%d%d%d", &a[1].id, &a[1].math,
			&a[1].programming, &a[1].total);//读liberal取一组成绩放到a[1]
		fprintf(science_liberal, "%ld\t\t%d\t%d\t%d\t%d\t%d\n",
			a[0].id, a[0].math, a[0].programming,
			a[1].math, a[1].programming, a[0].total + a[1].total);
		//两组成绩相加输出到science_liberal
	}

    fclose(science);
    fclose(liberal);
    fclose(science_liberal);
}
int main() {
	t2();
	t3();
	t4();
	t5();
	return 0;
}

6

dfahoadhfaoiOAIJD  SOIFSDIOdajiofio6f54er
G54df5zg
7thrs7h
7th6r4tj684y8t9j48dg456465484F86S4G 8RS48ds4f
sdg
sd7
end
000

结果:dfahoadhfaoiOAIJDSOIFSDIOdajiofioferGdfzgthrshthrtjytjdgFSGRSdsfsdgsdend

65454577764684894845646548486484847000

#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main() {
	FILE* f1;
	f1 = fopen("D:\\新建文本文档.txt", "r");
	if (!f1) {
		printf("cannot open file 新建文本文档.txt");
		exit(1);
	}
	FILE* f2;
	FILE* f3;
	f2 = fopen("D:\\zifu.txt", "w");
	if (!f1) {
		printf("cannot open file zifu.txt");
		exit(1);
	}
	f3 = fopen("D:\\shuzi.txt", "w");
	if (!f1) {
		printf("cannot open file shzi.txt");
		exit(1);
	}
	char c;
	while (!feof(f1)) {
		c = fgetc(f1);
		if (isalnum(c))
			if (isdigit(c))
				fputc(c, f3);
			else
				fputc(c, f2);
		//isalpha()isdigit()isalnum()分别为判断是否为字母,十进制数字,字母和数字
		//文件中不能使用汉字,因为isanum()等函数只能判断ASCII码
	}
	fclose(f1);
	fclose(f2);
	fclose(f3);
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值