实验5

实验任务1:文本文件数据格式化读写

(1)

// 将图书信息写入文本文件data1.txt

#include <stdio.h>
#define N 5
#define M 80

typedef struct
{
    char name[M]; // 书名
    char author[M]; // 作者
}Book;


int main()
{
    Book x[N] = { {"一九八四", "乔治.奥威尔"},
                  {"美丽新世界", "赫胥黎"},
                  {"昨日的世界", "斯蒂芬.茨威格"},
                  {"万历十五年", "黄仁宇"},
                  {"一只特立独行的猪", "王小波"}
                };
    int i;

    FILE *fp;

// 以写的方式打开文本文件data1.txt
    fp = fopen("data1.txt", "w");

// 如果打开文件失败,输出提示信息并返回
    if(fp == NULL)
{
            printf("fail to open file\n");
            return 1;
} 

// 将结构体数组x中的图书信息写到fp指向的文件data1.txt
// 同时也输出到屏幕上
    for(i=0; i<N; ++i)
{
            fprintf(fp, "%-20s %-20s\n", x[i].name, x[i].author);
            printf("%-20s %-20s\n", x[i].name, x[i].author);
}

    fclose(fp);

    return 0;
}

  

 

实验结果:

 

 

(2)

#include <stdio.h>
#define N 5
#define M 80
typedef struct
{
char name[M]; // 书名
char author[M]; // 作者
}Book;
int main()
{
Book x[N];
int i;
FILE *fp;
// 以读的方式打开文本文件data1.txt
fp = fopen("data1.txt", "r");
// 如果打开文件失败,输出提示信息并返回
if(fp == NULL)
{
printf("fail to open file\n");
return 1;
}
// 从fp指向的文件data1.txt中读取信息到结构体数组x
// 同时,把x的内容输出到屏幕上
for(i=0; i<N; ++i)
{
fscanf(fp, "%s %s\n", x[i].name, x[i].author);
printf("%-20s %-20s\n", x[i].name, x[i].author);
}
fclose(fp);
return 0;
}

  

运行结果:

回答:

因为它本身就是地址符,所以不需要加&

 

实验任务2:二进制文件数据块读写

#include <stdio.h>
#define N 5
#define M 80
typedef struct
{
char name[M]; // 书名
char author[M]; // 作者
}Book;
int main()
{
Book x[N] = { {"一九八四", "乔治.奥威尔"},
{"美丽新世界", "赫胥黎"},
{"昨日的世界", "斯蒂芬.茨威格"},
{"万历十五年", "黄仁宇"},
{"一只特立独行的猪", "王小波"}
};
int i;
FILE *fp;
// 以写的方式打开二进制文件data2.dat
fp = fopen("data2.dat", "wb");
// 如果打开文件失败,输出提示信息并返回
if(fp == NULL)
{
printf("fail to open file\n");
return 1;
}
// 将结构体数组x中的图书信息写以数据块方式写入文件
// 把从地址x处开始sizeof(Book)×N个字节大小的数据块写入fp指向的文件
fwrite(x, sizeof(Book), N, fp);
fclose(fp);
return 0;
}

  

#include <stdio.h>
#define N 5
#define M 80
typedef struct
{
char name[M]; // 书名
char author[M]; // 作者
}Book;
int main()
{
Book x[N];
int i;
FILE *fp;
// 以读的方式打开二进制文件data2.dat
fp = fopen("data2.dat", "rb");
// 如果打开文件失败,输出提示信息并返回
if(fp == NULL)
{
printf("fail to open file\n");
return 1;
}
// 从fp指向的文件中读取数据块到x对应的地址单元
// 数据块大小为sizeof(Book)×N
fread(x, sizeof(Book), N, fp);
// 在屏幕上输出结构体数组x中保存的数据
for(i=0; i<N; ++i)
printf("%-20s%-20s\n", x[i].name, x[i].author);
fclose(fp);
return 0;
}

  

可以

 

实验任务3:文件字符读写与字符数统计

 

#include <stdio.h>
int main()
{
    int i,c=0;
    char ch;
    FILE *fin;
    fin = fopen("data3_1.txt","r");
    
    if(fin == NULL) 
    { 
       printf("fail to open data3_1.txt\n"); 
       return 1; 
    }
    
    while(!feof(fin))
    {
        ch = fgetc(fin);
        if(ch!=' '&&ch!='\t'&&ch!='\n'&&ch!=EOF)
        c++;
    }
    fclose(fin);
    
    printf("data3_1.txt中共包含字符数:%d个",c);
    
    return 0; 
}

  

运行结果:

 

实验任务5:结构体与文件综合应用2

 

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define N 10
typedef struct
{
long int id;
char name[20];
float objective;
float subjective; /*操作题得分*/
float sum;
char level[10];
} STU;
// 函数声明
void input(STU s[], int n);
void output(STU s[], int n);
void process(STU s[], int n);
int main()
{
STU stu[N];
printf("从文件读入%d个考生信息: 准考证号,姓名,客观题得分(<=40),操作题得分(<=60)\n", N);
input(stu, N);
printf("\n对考生信息进行处理: 计算总分,确定等级\n");
process(stu, N);
printf("\n打印考生完整信息, 并保存到文件中");
output(stu, N);
return 0;
}
// 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void input(STU s[], int n)
{
int i;
FILE *fin;
fin = fopen("examinee.txt", "r");
if (fin == NULL)
{
printf("fail to open file\n");
exit(0);
}
while (!feof(fin))
{
for (i = 0; i < n; i++)
fscanf(fin, "%ld %s %f %f", &s[i].id, s[i].name,
&s[i].objective, &s[i].subjective);
}
fclose(fin);
}
//输出考生完整信息: 准考证号,姓名,客观题得分,操作题得分,总分,等级
// 不仅输出到屏幕上,还写到文本文件result.txt中
void output(STU s[], int n)
{
FILE *fout;
int i;
// 输出到屏幕
printf("\n");
printf("准考证号\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
for (i = 0; i < n; i++)
printf("%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].level);
// 保存到文件
fout = fopen("result.txt", "w");
if (!fout)
{
printf("fail to open or create result.txt\n");
exit(0);
}
fprintf(fout, "准考证号\t\t姓名\t客观题得分\t操作题得分\t总分\t\t等级\n");
for (i = 0; i < n; i++)
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].level);
fclose(fout);
}
// 对考生信息进行处理:计算总分,排序,确定等级
void process(STU s[], int n)
{
 int i,j,k;
    float Y,L;
    STU temp;
    for(i=0;i<n;i++)
        s[i].sum=s[i].objective+s[i].subjective;
    for(i=1;i<n;i++)
        for(j=0;j<i;j++)
        {
            if(s[i].sum>s[j].sum)
            {
                k=j;
                temp=s[i];
                for(j=i-1;j>=k;j--)
                    s[j+1]=s[j];
                s[k]=temp;
            }
        }
    Y=s[(int)(n*0.1-1)].sum;
    L=s[(int)(n*0.5-1)].sum;
    for(i=0;i<n;i++)
        if(s[i].sum>=Y)
            strcpy(s[i].level,"优秀");
        else if(s[i].sum>=L)
            strcpy(s[i].level,"合格");
        else
            strcpy(s[i].level,"不合格");
}

  

运行结果:

 

实验任务6:文件综合应用3

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define M 80
#define N 5
typedef struct
{
    long int id;
    char name[20];
    char classes[80];
    int target;
} STU;
void input(STU s[], int n);
void random(STU s[],STU lucky[],int m,int n);
void output(STU lucky[], int n);
int main()
{
    STU stu[M];
    STU lucky[N];
    input(stu, M);
    random(stu,lucky,M,N);
    output(lucky,N);
    return 0;
}
void input(STU s[], int n)
{
    int i;
    FILE *fin;
    fin = fopen("list.txt", "r");
    if (fin == NULL)
    {
        printf("fail to open file\n");
        exit(0);
    }
    while (!feof(fin))
    {
        for (i = 0; i < n; i++)
            fscanf(fin, "%ld %s %s", &s[i].id, s[i].name,
                   s[i].classes);
    }
    fclose(fin);
}
void random(STU s[],STU lucky[],int m,int n)
{
    int x[m];
    int k = 0;
    srand(time(NULL));
    while (k < n)
    {
        int i = rand()%m + 1;
        if(x[i] == 1)
            continue;
        lucky[k] = s[i];
        x[i] = 1;
        k++;
    }
}
void output(STU lucky[], int n)
{
    for (int i = 0; i < 5; i++)
        printf("%ld\t%-10s %-10s\n", lucky[i].id, lucky[i].name, lucky[i].classes);
    FILE *fout;
    fout = fopen("lucky2.txt", "w");
    for (int i = 0; i < n; i++)
        fprintf(fout, "%ld\t%-10s %-10s\n", lucky[i].id, lucky[i].name, lucky[i].classes);
    fclose(fout);
}

  

实验结果:

 

posted @ 2022-06-06 19:14  这很不二  阅读(10)  评论(2编辑  收藏  举报