高级语言程序设计第十次作业

这个作业属于哪个课程:https://edu.cnblogs.com/campus/fzu/gjyycx
这个作业要求在哪里:https://edu.cnblogs.com/campus/fzu/gjyycx/homework/15596
学号:102500417
姓名:刘朝榕
设计程序
1编写一个程序,将一个文件的内容复制到另一个文件中。

点击查看代码
#include<stdio.h>
#include<stdlib.h>
int main(void){
	FILE*p=NULL;
	FILE*q=NULL;
	p=fopen("aaa.txt","rb");
	if(p==NULL){
		printf("文件a打开失败\n");
		exit(1);
	}
	q=fopen("bbb.txt","wb");
	if(q==NULL){
		printf("文件b打开失败\n");
		exit(1);
	}
	int c;
	while((c=fgetc(p))!=EOF){
		fputc(c,q);
	}
	fclose(p);
	fclose(q);
	p=NULL;
	q=NULL;
	printf("文件复制成功");
} 
运行程序前

屏幕截图 2025-12-16 173506

屏幕截图 2025-12-16 173514
运行程序后
屏幕截图 2025-12-16 173540
程序截图
屏幕截图 2025-12-16 173612

2编写一个程序,统计一个文本文件中的字符数。

点击查看代码
#include<stdio.h>
#include<stdlib.h>
int main(void){
	FILE*p=NULL;
	int c;
	int i=0; 
	p=fopen("aaa.txt","rb");
	if(p==NULL){
		printf("文件a打开失败\n");
		exit(1);
	}
	while((c=fgetc(p))!=EOF){
		i++; 
	}
	fclose(p);
	p=NULL;;
	printf("%d",i);
} 

屏幕截图 2025-12-16 173506

屏幕截图 2025-12-16 174609

3编写一个程序,读取一个文本文件的内容,并在控制台上显示。

点击查看代码
#include<stdio.h>
#include<stdlib.h>
int main(void){
	FILE*p=NULL;
	int c;
	p=fopen("第十次作业设计程序文本.txt","rb");
	if(p==NULL){
		printf("文件a打开失败\n");
		exit(1);
	}
	while((c=fgetc(p))!=EOF){
		printf("%c",c); 
	}
	fclose(p);
	p=NULL;;
} 

屏幕截图 2025-12-16 175428

屏幕截图 2025-12-16 175413

4编写一个程序,向一个文本文件的末尾追加一行文本。

点击查看代码
#include<stdio.h>
#include<stdlib.h>
int main(void){
	FILE*p=NULL;
	char c[]="Hello world";
	p=fopen("aaa.txt","a");
	if(p==NULL){
		printf("文件a打开失败\n");
		exit(1);
	}
	fputs(c,p);
	fclose(p);
	p=NULL;
	printf("追加文本成功");
} 
追加文本前

屏幕截图 2025-12-16 173506
追加文本后

屏幕截图 2025-12-16 180158
程序截图

屏幕截图 2025-12-16 180301

5编写一个程序,读取一个文本文件,删除文件中的特定行(例如,包含特定单词的行),并将结果保存到新文件中。

点击查看代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h> 
#define MAX 1024
int main(void){
	FILE*p=NULL;
	FILE*q=NULL;
	int c;
	const char s[]="word";
	char line[MAX];
	p=fopen("ccc.txt","rb");
	q=fopen("ddd.txt","wb");
	if(p==NULL){
		printf("文件c打开失败\n");
		exit(1);
	}
	if(q==NULL){
		printf("文件d打开失败\n");
		exit(1);
	}
	while(fgets(line,MAX,p)!=NULL){
		if(strstr(line,s)==NULL){
			fputs(line,q);
		}
	}
	fclose(p);
	fclose(q);
	p=NULL;
	q=NULL; 
	printf("程序运行成功");
} 
运行程序前

屏幕截图 2025-12-16 182042

屏幕截图 2025-12-16 182052

运行程序后

屏幕截图 2025-12-16 182410

程序截图

屏幕截图 2025-12-16 182431
6计算并显示一个文件的大小(以字节为单位),要求使用ftell。

点击查看代码
#include<stdio.h>
#include<stdlib.h>
int main(void){
	FILE*p=NULL;
	long t; 
	p=fopen("ccc.txt","rb");
	if(p==NULL){
		printf("文件c打开失败\n");
		exit(1);
	}
	fseek(p,0L,SEEK_END);
	t=ftell(p); 
	fclose(p);
	p=NULL;
	printf("程序运行成功\n");
	printf("文件大小为:%ld字节",t);
} 

屏幕截图 2025-12-16 184030
7有五个学生,每个学生有 3 门课的成绩,从键盘输入以上数据(包括学号,姓名,三门课成绩),计算出平均成绩,将原有的数据和计算出的平均分数存放在磁盘文件"student.txt"中。

点击查看代码
#include<stdio.h>
#include<stdlib.h>
struct student{
	int n;
	char c[100];
	double cj1;
	double cj2;
	double cj3;
	double average;
};
int main(void){
	FILE*p=NULL; 
	p=fopen("eee.txt","w");
	if(p==NULL){
		printf("文件e打开失败\n");
		exit(1);
	}
	struct student stu[5];
	for(int i=0;i<5;i++){
		scanf("%d %s %lf %lf %lf ",&stu[i].n,stu[i].c,&stu[i].cj1,&stu[i].cj2 ,&stu[i].cj3);
		stu[i].average=(stu[i].cj1+stu[i].cj2+stu[i].cj3)/3;
		fprintf(p,"%d\t",stu[i].n);  
		fprintf(p,"%s\t",stu[i].c);
		fprintf(p,"%.1lf\t",stu[i].cj1);
		fprintf(p,"%.1lf\t",stu[i].cj2);
		fprintf(p,"%.1lf\t",stu[i].cj3);
		fprintf(p,"%.1lf\n",stu[i].average);
	}
	fclose(p);
	p=NULL;
	printf("程序运行成功");
} 
运行程序前

屏幕截图 2025-12-16 220748

运行程序后
屏幕截图 2025-12-16 224750

程序截图
屏幕截图 2025-12-16 224815

posted @ 2025-12-16 22:52    阅读(2)  评论(0)    收藏  举报