5.6作业

文章描述了一个C语言程序,通过文件I/O操作读取和写入学生信息,包括姓名、年龄和各科成绩,并在屏幕上打印输出。
摘要由CSDN通过智能技术生成
ubuntu@ubuntu:D6$ cat p4_homework.c
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <pthread.h>
#include <semaphore.h>
#include <wait.h>
#include <signal.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <semaphore.h>
#include <sys/msg.h>
#include <sys/shm.h>
#include <sys/un.h>


typedef struct Student {
		char name[20];
		int age;
		double math_score;
		double chinese_score;
		double english_score;
		double physics_score;
		double chemistry_score;
		double bio_score;
}Student;
void printfStudentInfo(Student *pStu,int len){
	for (int i=0;i<len;++i){
			printf("姓名:%s\t年龄:%d\t数学:%2lf\t语文:%2lf\t英语:%2lf\t物理:%2lf\t化学:%2lf\t生物:%2lf\n",
			(pStu+i)->name,(pStu+i)->age,(pStu+i)->math_score,(pStu+i)->chinese_score,(pStu+i)->english_score,(pStu+i)->physics_score,(pStu+i)->chemistry_score,(pStu+i)->bio_score);
	}

}
int main(int argc, const char *argv[])
{
	Student students [] ={	
			{"Tom",25,90,91,92,93,94,95},
			{"Li",23,90,90,90,93,91,92},
			{"Zhao",22,99,98,90,93,90,95},
	 		{"Tian",24,79,91,92,90,99,92},
			{"Xing",26,90,90,89,92,91,95},
	 };
	Student students2 [5]={};

		char ch;
		FILE *fp=fopen("stu.txt","w");
		ch=fgetc(fp);
		int n=5;
	
		for (int i=0;i<n;++i){
			fprintf(fp,"%s\t%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",students[i].name,students[i].age,students[i].math_score,
				students[i].chinese_score,students[i].english_score,students[i].physics_score,students[i].chemistry_score,students[i].bio_score);
		}
		fclose(fp);
		fp=fopen("stu.txt","r");
		for (int j=0;j<n;++j)
		{
			fscanf(fp,"%s\t%d\t%lf\t%lf\t%lf\t%lf\t%lf\t%lf\n",students2[j].name,&students2[j].age,&students2[j].math_score,
				&students2[j].chinese_score,&students2[j].english_score,&students2[j].physics_score,&students2[j].chemistry_score,&students2[j].bio_score);
		
		}
		fclose(fp);
		printfStudentInfo(students2,sizeof(students2)/sizeof(students2[0]));
	
	
	return 0;
}
ubuntu@ubuntu:D6$ vim p4_homework.c
ubuntu@ubuntu:D6$ gcc p4_homework.c
ubuntu@ubuntu:D6$ ./a.out
姓名:Tom	年龄:25	数学:90.000000	语文:91.000000	英语:92.000000	物理:93.000000	化学:94.000000	生物:95.000000
姓名:Li	年龄:23	数学:90.000000	语文:90.000000	英语:90.000000	物理:93.000000	化学:91.000000	生物:92.000000
姓名:Zhao	年龄:22	数学:99.000000	语文:98.000000	英语:90.000000	物理:93.000000	化学:90.000000	生物:95.000000
姓名:Tian	年龄:24	数学:79.000000	语文:91.000000	英语:92.000000	物理:90.000000	化学:99.000000	生物:92.000000
姓名:Xing	年龄:26	数学:90.000000	语文:90.000000	英语:89.000000	物理:92.000000	化学:91.000000	生物:95.000000

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值