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

实验7 文件应用编程

时间:2023-12-17 10:34:09浏览次数:32  
标签:fp 文件 return int 编程 st 实验 printf txt

1.实验任务1

源代码

 1 // 将图书信息写入文本文件data1.txt
 2  
 3 #include <stdio.h>
 4 
 5 #define N 80
 6 
 7 typedef struct {
 8     char name[N];   // 书名 
 9     char author[N]; // 作者 
10 } Book;
11 
12 int main() {
13     Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"},
14                   {"《灯塔》", "克里斯多夫.夏布特"},
15                   {"《五号屠宰场》", "库尔特.冯内古特"}, 
16                   {"《出卖月亮的人》", "罗伯特.海因莱茵"},
17                   {"《大地之上》", "罗欣顿·米斯特里"}, 
18                   {"《上学记》", "何兆武"}, 
19                   {"《命运》", "蔡崇达"} };
20     int n, i;
21     FILE *fp;
22 
23     // 计算数组x中元素个数
24     n = sizeof(x) / sizeof(Book);       
25     
26     // 以写的方式打开文本文件data1.txt 
27     fp = fopen("data1.txt", "w");
28     
29     // 如果打开文件失败,输出提示信息并返回 
30     if(fp == NULL) {
31         printf("fail to open file\n");
32         return 1;
33     }
34     
35     // 将结构体数组x中的图书信息写到fp指向的文件data1.txt
36     // 同时也输出到屏幕上 
37     for(i = 0; i < n; ++i){
38         fprintf(fp, "%-20s %-20s\n", x[i].name, x[i].author);
39         printf("%-20s %-20s\n", x[i].name, x[i].author);
40     }
41     
42     fclose(fp);
43     
44     return 0;
45 }
task1_1.c

源代码

 1 // 从文本文件data1.txt中读取图书信息,并打印输出到屏幕 
 2  
 3 #include <stdio.h>
 4 #define N 80
 5 #define M 100
 6 
 7 typedef struct {
 8     char name[N];   // 书名 
 9     char author[N]; // 作者 
10 } Book;
11 
12 
13 int main() {
14     Book x[M]; 
15     int i, n;
16     
17     FILE *fp;
18     
19     // 以读的方式打开文本文件data1.txt 
20     fp = fopen("data1.txt", "r");
21     
22     // 如果打开文件失败,输出提示信息并返回 
23     if(fp == NULL) {
24         printf("fail to open file\n");
25         return 1;
26     }
27     
28     // 从文件中读取图书信息,保存到结构体数组x中
29    i = 0;
30     while(!feof(fp)) {
31         fscanf(fp, "%s%s", x[i].name, x[i].author);
32         ++i;
33     }
34     n = i-1;
35 
36     // 将图书信息打印输出到屏幕上
37     for(i = 0; i < n; ++i)
38         printf("%d. %-20s%-20s\n", i+1, x[i].name, x[i].author);
39     
40     fclose(fp);
41     
42     return 0;
43 }
task1_2.c

 

2.实验任务2

源代码

 1 // 将图书信息写入二进制文件data2.dat
 2  
 3 #include <stdio.h>
 4 #define N 80
 5 #define M 7
 6 
 7 typedef struct {
 8     char name[N];   // 书名 
 9     char author[N]; // 作者 
10 } Book;
11 
12 int main() {
13     Book x[M] = { {"《雕塑家》", "斯科特.麦克劳德"},
14                   {"《灯塔》", "克里斯多夫.夏布特"},
15                   {"《五号屠宰场》", "库尔特.冯内古特"}, 
16                   {"《出卖月亮的人》", "罗伯特.海因莱茵"},
17                   {"《大地之上》", "罗欣顿·米斯特里"}, 
18                   {"《上学记》", "何兆武"}, 
19                   {"《命运》", "蔡崇达"} };
20     int n, i;
21     
22     FILE *fp;
23  
24     
25     // 以写的方式打开二进制文件data2.dat 
26     fp = fopen("data2.dat", "wb");
27     
28     // 如果打开文件失败,输出提示信息并返回 
29     if(fp == NULL) {
30         printf("fail to open file\n");
31         return 1;
32     }
33     
34     // 将结构体数组x中的图书信息以数据块方式写入文件data2.dat
35     fwrite(x, sizeof(Book), M, fp);
36     
37     fclose(fp);
38     
39     return 0;
40 }
task2_1.c

源代码

 1 // 从二进制文件data2.dat读取图书信息,并打印输出到屏幕
 2  
 3 #include <stdio.h>
 4 #define N 80
 5 #define M 7
 6 
 7 typedef struct {
 8     char name[N];   // 书名 
 9     char author[N]; // 作者 
10 } Book;
11 
12 int main() {
13     Book x[M];
14     int i;
15     
16     FILE *fp;
17     
18     // 以读的方式打开二进制文件data2.dat 
19     fp = fopen("data2.dat", "rb");
20     
21     // 如果打开文件失败,输出提示信息并返回 
22     if(fp == NULL) {
23         printf("fail to open file\n");
24         return 1;
25     }
26     
27     // 从文件data2.dat以数据块方式读入图书信息数据到结构体数组x
28     fread(x, sizeof(Book), M, fp);
29 
30     // 在屏幕上输出结构体数组x中存储的图书信息
31     for(i = 0; i < M; ++i)
32         printf("%d. %-20s%-20s\n", i+1, x[i].name, x[i].author);
33     
34     fclose(fp);
35     
36     return 0;
37 }
task2_2.c

源代码

 1 // 从二进制文件data2.dat读取图书信息,并打印输出到屏幕
 2  
 3 #include <stdio.h>
 4 #define N 80
 5 #define M 100
 6 
 7 typedef struct {
 8     char name[N];   // 书名 
 9     char author[N]; // 作者 
10 } Book;
11 
12 int main() {
13     Book x[M];
14     int i, cnt;
15     
16     FILE *fp;
17     
18     // 以读的方式打开二进制文件data2.dat 
19     fp = fopen("data2.dat", "rb");
20     
21     // 如果打开文件失败,输出提示信息并返回 
22     if(fp == NULL) {
23         printf("fail to open file\n");
24         return 1;
25     }
26     
27     // 从文件data2.dat以数据块方式读入图书信息数据到结构体数组x
28     i = 0;
29     while(!feof(fp)) {
30         fread(&x[i], sizeof(Book), 1, fp);
31         i++;
32     }
33     cnt = i - 1;
34     
35 
36     // 在屏幕上输出结构体数组x中存储的图书信息
37     for(i = 0; i < cnt; ++i)
38         printf("%d. %-20s%-20s\n", i+1, x[i].name, x[i].author);
39     
40     fclose(fp);
41     
42     return 0;
43 }
task2_3.c

 

3.实验任务3

源代码

 1 // 使用fputs()将字符串写入文本文件
 2 
 3 #include <stdio.h>
 4 #define N 5
 5 
 6 int main() {
 7     // 定义字符指针数组,每个元素存放字符串的起始地址
 8     char *ptr[N] = { "Working\'s Blues",
 9                      "Everything Will Flow",
10                      "Streets of London",
11                      "Perfect Day",
12                      "Philadelphia"};
13     int i;
14     FILE *fp;
15 
16     fp = fopen("data3.txt", "w");
17     if(fp == NULL) {
18         printf("fail to open file\n");
19         return 1;
20     }
21 
22     for(i = 0; i < N; ++i) {
23         fputs(ptr[i], fp);  // 把ptr[i]指向的字符串写入fp指向的文件中
24         fputs("\n", fp);
25     }
26     
27     fclose(fp);
28     return 0;
29 }
task3_1.c

源代码

 1 // 使用fgets()从文件中读取字符串并在屏幕上显示
 2 
 3 #include <stdio.h>
 4 #define N 5
 5 #define M 80
 6 
 7 int main() {
 8     char songs[N][M];
 9     int i;
10     FILE *fp;
11 
12     fp = fopen("data3.txt", "r");
13     if(fp == NULL) {
14         printf("fail to open file\n");
15         return 1;
16     }
17 
18     for(i = 0; i < N; ++i)
19         fgets(songs[i], 80, fp);
20 
21     for(i = 0; i < N; ++i)
22         printf("%d. %s", i+1, songs[i]);
23     
24     fclose(fp);
25     return 0;
26 }
task3_2.c

源代码

 1 #include <stdio.h>
 2 
 3 int main() {
 4     char ch;
 5     FILE *fp;
 6 
 7     fp = fopen("data3.txt", "r");
 8     if(fp == NULL) {
 9         printf("fail to open file\n");
10         return 1;
11     }
12 
13     while(1) {
14         ch = fgetc(fp);
15         if(ch == EOF)
16             break;
17         
18         putchar(ch);
19     }
20 
21     fclose(fp);
22     return 0;
23 }
task3_3.c

 

4.实验任务4

源代码

 1 #include<stdio.h>
 2 
 3 int main()
 4 {
 5     FILE *fp;
 6     char ch;
 7     int count=0;
 8     fp=fopen("data4.txt","r");
 9     if(fp==NULL)
10     {
11         printf("error\n");
12         return 1;
13     }
14     while(ch!=EOF)
15     {
16         ch=fgetc(fp);
17         if(ch==EOF)
18             break;
19         else if(ch==' '||ch=='\n')
20             continue;
21         else
22             count++;
23     }
24     
25     fclose(fp);
26     printf("data4.txt中共包含字符数(不含空白符):%d\n",count);
27     return 0;
28     
29 }
task4.c

 

5.实验任务5

源代码

  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, N);      // 输出到屏幕
 35     foutput(stu, N);    // 输出到文件
 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 
 95 // 对考生信息进行处理:计算每位考生考试总分、结果;统计考试通过的人数
 96 int process(STU st[], int n, STU st_pass[]) 
 97 {
 98     FILE *fp;
 99     int t=0;
100     int i;
101     fp=fopen("list_pass.txt","r");
102     for(i=0;i<n;i++) 
103         st[i].sum =st[i].objective+st[i].subjective;
104     for(i=0;i<n;i++) 
105     {
106         if(st[i].sum>=60)
107         {
108             strcpy(st[i].ans,"pass");
109             t++;
110         }
111         else
112             strcpy(st[i].ans,"fail");
113     }
114      return t;
115 }
task5.c

 

6.实验任务6

源代码

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 #define N 20 
 4 #define M 80
 5 
 6 typedef struct {
 7     int xuehao;
 8     char name[N];
 9     char class[N];
10 }stu;
11 
12 int main()
13 {
14     stu st[M];
15     FILE *fp;
16     int i;
17     int n;
18     fp=fopen("list.txt","r");
19 
20     if(fp==NULL) 
21     {
22         printf("error\n");
23         return 1;
24     }
25     
26     for(i=0;i<M;i++)
27         fscanf(fp,"%d\t%s\t%s\n",&st[i].xuehao,st[i].name,st[i].class);
28     fclose(fp);
29     
30     fp=fopen("lucky.txt","w");
31     srand(time(NULL)); 
32     for(i=0;i<5;i++)
33     {
34         n=rand()%50;
35         fprintf(fp,"%d\t%s\t%s\n",st[n].xuehao,st[n].name,st[n].class);
36         printf("%d\t%s\t%s\n",st[n].xuehao,st[n].name,st[n].class);
37     }
38     
39     fclose(fp);
40     
41     return 0;
42 
43 }
task6.c

 

标签:fp,文件,return,int,编程,st,实验,printf,txt
From: https://www.cnblogs.com/ojoyo/p/17901638.html

相关文章

  • 微信小程序代码构成及每一个代码文件的作用(附图)
    一、微信小程序有四种类型文件:1.JSON配置文件(.json后缀)2.WXML模板文件(.wxml后缀)3.WXSS样式文件(.wxss后缀)4.JS脚本逻辑文件(.js后缀) 二、JSON配置:1.JSON配置:JSON是一种数据格式,用来静态配置。app.json 小程序配置project.config.json 工具配置 page.json页面......
  • 实验7
    task41#include<stdio.h>23intmain(){45inti=0;6chars;78FILE*fp;9fp=fopen("data4.txt","r");1011while(1){12s=fgetc(fp);13if(s==EOF){14......
  • DELPHI模板编程
    DELPHI模板编程procedureTCRUD<T>.execsql(OnTableModel:TTableModel);//执行事务性SQLbeginifreq.Body=nilthenExit;varpool:TDBPool:=GetDBPool(dbid);//databasepooldb:=pool.Lock;trytrytable:=serialize.TSerial<TTab......
  • vscode编译多个C/CPP文件
    修改vscode里面的tasks.json文件,下面是修改好的,参考"args":["-fdiagnostics-color=always","-g",//"${file}", //只执行当前文件"${workspaceFolder}\\*.cpp",//工作区内,执行多个关联cpp文件,但只有一个main()......
  • 实验6
    任务41#include<iostream>2#include"Vector1.hpp"34voidtest(){5usingnamespacestd;67intn;8cin>>n;910Vector<double>x1(n);11for(autoi=0;i<n;++i)12x1......
  • 实验6 模板类、文件I/O和异常处理
    实验任务41#defineVECTOR_HPP2#include<iostream>3#include<stdexcept>4usingnamespacestd;5template<typenameT>6classVector{7private:8T*data;9size_tsize;10public:11Vector(size_ta):size(a......
  • 实验三-电子公文传输系统1-个人贡献
    实验三-电子公文传输系统1-个人贡献任务详情1简述你完成的工作2你们小组总共的代码行数,你贡献的代码行数?相关代码链接?3你们小组总共的文档数?你贡献的文档数?相关链接?主要完成的工作个人主要完成的工作是扮演了项目经理和产品经理的角色,具体工作如下:项目经理:根据项目......
  • 实验六 模板类、文件I/O和异常处理
    实验任务4Vector.hpp:1#pragmaonce23#include<iostream>4#include<stdexcept>56usingnamespacestd;78template<typenameT>9classVector10{11private:12intsize;13T*ptr;14public:1516Vector(in......
  • 文件上传
    文件上传流程:客户端将文件数据发送给服务器服务器保存上传的文件数据到服务器端服务器响应给客户端一个文件访问地址测试地址:http://xxx/api/upload键的名称(表单域名称):imagefile请求方法:POST请求的表单格式:multipart/form-data请求体中必须包含一个键值对,键的名称是......
  • 实验三-电子公文传输系统-个人贡献
    (一)简述你完成的工作我的工作主要是项目整体结构的搭建设计,和公文系统功能的实现一mvc模式和服务实现逻辑链设计在设计初期,我们确定好了分工和系统编写的基调。我认为电子公文系统中,可以采用MVC模式进行设计,得到了其他组员的支持,我们的分工也基本根据这个方式而来。其中我主要......