首页 > 编程语言 >实验7 文件应用编程

实验7 文件应用编程

时间:2024-06-17 21:12:00浏览次数:15  
标签:printf 文件 int 编程 st STU 实验 pass fin

task4.c

 1 #include <stdio.h>
 2 
 3 int main(){
 4     int count=0;
 5     char ch;
 6     FILE *fp;
 7 
 8     fp = fopen("data4.txt", "r");
 9     if(fp == NULL) {
10         printf("fail to open file\n");
11         return 0;
12     }
13     while(!feof(fp)){
14         ch = fgetc(fp);
15         if(ch!=' '&& ch!='\n' && ch!=EOF)
16             count++;
17     }
18         fclose(fp);
19     
20     printf("data4.txt中共包含字符数(不计空白符):%d",count);
21     return 0;
22 }
23     

 

task5.c

  1 #include <stdio.h>
  2 #include <string.h>
  3 #include <stdlib.h>
  4 
  5 #define N 10
  6 
  7 typedef struct {
  8     long int id;
  9     char name[20];
 10     float objective;    // 客观题得分
 11     float subjective;   // 操作题得分
 12     float sum;          // 总分
 13     char ans[10];       // 考试结果
 14 } STU;
 15 
 16 // 函数声明
 17 void finput(STU st[], int n);
 18 void foutput(STU st[], int n);
 19 void output(STU st[], int n);
 20 int process(STU st[], int n, STU st_pass[]);
 21 
 22 int main() {
 23     STU stu[N], stu_pass[N];
 24     int cnt;
 25     double pass_rate;
 26 
 27     printf("从文件读入%d个考生信息...\n", N);
 28     finput(stu, N);
 29 
 30     printf("\n对考生成绩进行统计...\n");
 31     cnt = process(stu, N, stu_pass);
 32 
 33     printf("\n通过考试的名单:\n");
 34     output(stu_pass, cnt);      // 输出到屏幕
 35     foutput(stu_pass, cnt);    // 输出到文件
 36 
 37     pass_rate = 1.0 * cnt / N;
 38     printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
 39 
 40     return 0;
 41 }
 42 
 43 // 把通过考试的考生完整信息输出到屏幕上
 44 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 45 void output(STU st[], int n) {
 46     int i;
 47     
 48     printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 49     for (i = 0; i < n; i++)
 50         printf("%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].ans);
 51 }
 52 
 53 // 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
 54 void finput(STU st[], int n) {
 55     int i;
 56     FILE *fin;
 57 
 58     fin = fopen("examinee.txt", "r");
 59     if (fin == NULL) {
 60         printf("fail to open file\n");
 61         exit(0);
 62     }
 63 
 64     while (!feof(fin)) {
 65         for (i = 0; i < n; i++)
 66             fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
 67     }
 68 
 69     fclose(fin);
 70 }
 71 
 72 // 把通过考试的考生完整信息写入文件list_pass.txt
 73 // 准考证号,姓名,客观题得分,操作题得分,总分,结果
 74 void foutput(STU s[], int n) {
 75     FILE *fout;
 76     int i;
 77     
 78     // 保存到文件 
 79     fout = fopen("list_pass.txt", "w");
 80     if (!fout) {
 81         printf("fail to open or create list_pass.txt\n");
 82         exit(0);
 83     }
 84     
 85     fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t结果\n");
 86 
 87     for (i = 0; i < n; i++)
 88         fprintf(fout, "%ld\t\t%s\t%.2f\t\t%.2f\t\t%.2f\t\t%s\n", s[i].id, s[i].name, s[i].objective, s[i].subjective, s[i].sum, s[i].ans);
 89 
 90     fclose(fout);
 91 }
 92 
 93 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 94 int process(STU st[], int n, STU st_pass[]) {
 95    // 待补足
 96    int i,k=0;
 97    for(i=0;i<n;i++){
 98         st[i].sum=st[i].objective+st[i].subjective;
 99         if(st[i].sum>=60){
100             strcpy(st[i].ans,"pass");
101             st_pass[k++]=st[i];
102         }
103         else
104             strcpy(st[i].ans,"fail");
105    }
106    return k;
107 }

 

task6.c

 1 #include <stdio.h>
 2 #include <time.h>
 3 #include <stdlib.h>
 4 
 5 #define N 80
 6 #define M 100
 7 typedef struct {
 8     long int id;
 9     char name[10];
10     char class[20];
11 }STU;
12 
13 
14 int main(){
15     STU st[M];
16     int i,n;
17     FILE *fin;
18     FILE *fp;
19 
20     fin = fopen("list.txt", "r");
21     if (fin == NULL) {
22         printf("fail to open file\n");
23         exit(0);
24     }
25     
26     while (!feof(fin)){
27         for(i=0;i<N;i++)
28         fscanf(fin,"%ld %s %s",&st[i].id,st[i].name,st[i].class);
29     }
30 
31     fp = fopen("lucky.txt", "wb");
32     if(fp == NULL) {
33         printf("fail to open file\n");
34         return 1;
35     }
36     
37     srand(time(0));
38     for(i=0;i<5;i++){
39     n=rand()%N;
40     fprintf(fp,"%ld %-10s %-20s\n",st[n].id,st[n].name,st[n].class);
41     printf("%ld %-10s %-20s\n",st[n].id,st[n].name,st[n].class);
42     }
43     
44     fclose(fin);
45     fclose(fp);
46     return 0;
47 }

 

实验总结

1.确保文件处于相对路径(与.c文件而言)

 

标签:printf,文件,int,编程,st,STU,实验,pass,fin
From: https://www.cnblogs.com/qq2055939904/p/18253201

相关文章

  • c++万能头文件
    一、问题出现c/C++使用首先就是要开头头文件的引用,没有写头文件的程序基本都不会成功运行得到想要的结果,因为每个程序基本都避免不了一定的输入与输出,而输入与输出却在头文件#include/#include<stdio.h>中大量的库函数扑面而来,随之产生了一个很令人头疼的问题,每一种类型的函......
  • (11.3)iic串口读写EEPROM实验:程序设计
    一、实验任务二、架构框图其中:i2c驱动模块: bit_ctrl:0代表发送8位字节地址;1代表发送16位字节地址(本实验采用)i2c_addr[15:0]:16位字节地址,当bit_ctrl为0时只有低8位是有效的i2c_data_w[7:0]:向EEPROM写入的8位数据i2c_exec:拉高代表当前进行......
  • Windows 中的 csc 服务是指 "Client Side Caching",即客户端缓存服务。这个服务主要用
    Windows中的csc服务是指"ClientSideCaching",即客户端缓存服务。这个服务主要用于离线文件和文件夹的同步,特别是在使用“离线文件”功能时。下面是关于csc服务的一些介绍:功能:csc服务允许用户在离线状态下访问网络共享文件和文件夹。当用户连接到网络时,csc服务会自动将......
  • 系统地学习Lua编程语言,掌握其基础知识并应用于实际项目中;Lua编程语言中级开发的大纲,涵
    学习Lua编程语言的初级开发大纲可以帮助你系统性地掌握这门语言的基础知识。下面是一个详细的大纲,从基础语法到基本的编程概念,再到一些实用的库和工具。Lua初级开发大纲1.Lua简介Lua的历史与特点Lua的应用场景安装与配置Lua环境2.基础语法注释:单行注释和多行注释......
  • 【杂记】配置文件
    properties配置文件application.properties是springboot项目默认的配置文件,所以springboot程序在启动时会默认读取application.properties配置文件,而我们可以使用一个现成的注解:@Value,获取配置文件中的数据。@Value注解通常用于外部配置的属性注入,具体用法为:@Value("${配置文......
  • NIO操作文件读写
    第一章第一节,Buffuer案例一 从buffur读出数据,创建了一个 FileInputStream 对象,并通过调用 getChannel() 方法获取了与之关联的 FileChannel。然后,我们创建了一个 ByteBuffer,并使用 channel.read(buffer) 方法从文件中读取数据到缓冲区。最后,我们通过循环将缓冲区中......
  • 实验7 文件应用编程
    task4.c1#include<stdio.h>2intmain()3{4FILE*fp;5charch;6intcount=0;7fp=fopen("data4.txt","r");8if(fp==NULL)9{10printf("failtoopenfile\n");11......
  • 【JavaWeb】文件上传
    前端代码<formaction="/upload"method="post"enctype="multipart/form-data"> 姓名:<inputtype="text"name="username"><br>年龄:<inputtype="text"name="age"><b......
  • dwm 文件上传/打开文件夹 白屏
    问题描述及解决我的chromium和code-oss在上传文件/打开文件夹时会出现白屏的情况,即打开文件管理器时发生。因为firefox没有出现这种情况,最先以为是chromium的bug,起先以为缺少包,遂安装gtk3,gtk4,qt5-base,qt6-base,然而问题没有解决,设置默认的文件管理器也无作用。后面发现是缩......
  • maven 加载不到 mybatis xml 配置文件
     <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>......