实验7

task1

// 文件读写操作:格式化读、写文本文件
#include <stdio.h>
#include <stdlib.h>

#define N 80
#define M 100

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

void write();
void read();

int main() {
    printf("测试1: 把图书信息写入文本文件\n");
    write();

    printf("\n测试2: 从文本文件读取图书信息, 打印输出到屏幕\n");
    read();

    system("pause");
    return 0;
}

void write() {
    Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"},
                  {"《灯塔》", "克里斯多夫.夏布特"},
                  {"《人的局限性》", "塞缪尔.约翰生"}, 
                  {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
                  {"《大地之上》", "罗欣顿·米斯特里"}, 
                  {"《上学记》", "何兆武"}, 
                  {"《命运》", "蔡崇达"} };
    int n, i;
    FILE *fp;

    // 计算数组x中元素个数
    n = sizeof(x) / sizeof(x[0]);       
    
    // 以写的方式打开文本文件data1.txt 
    fp = fopen("data1.txt", "w");
    
    // 如果打开文件失败,输出提示信息并返回 
    if(fp == NULL) {
        printf("fail to open file to write\n");
        return;
    }
    
    // 将结构体数组x中的图书信息格式化写到fp指向的文件data1.txt
    for(i = 0; i < n; ++i)
        fprintf(fp, "%-40s %-20s\n", x[i].name, x[i].author);
    
    fclose(fp);
}

void read() {
    Book x[M]; 
    int i, n;
    int number;
    
    FILE *fp;
    
    // 以读的方式打开文本文件data1.txt 
    fp = fopen("data1.txt", "r");
    
    // 如果打开文件失败,输出提示信息并返回 
    if(fp == NULL) {
        printf("fail to open file to read\n");
        return;
    }
    
    // 从文件中读取图书信息,保存到结构体数组x中
   i = 0;
    while(!feof(fp)) {
        number = fscanf(fp, "%s%s", x[i].name, x[i].author);
        if(number != 2)
            break;
        i++;
    }
    n = i;

    // 将图书信息打印输出到屏幕上
    for(i = 0; i < n; ++i)
        printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author);
    
    fclose(fp);
}

task2

// 文件读写操作:以数据块方式读、写二进制文件

#include <stdio.h>
#include <stdlib.h>

#define N 80
#define M 100

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

void write();
void read();

int main() {
    printf("测试1: 把图书信息以数据块方式写入二进制文件\n");
    write();

    printf("\n测试2: 从二进制文件读取图书信息, 打印输出到屏幕\n");
    read();

    system("pause");
    return 0;
}

void write() {
    Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"},
                  {"《灯塔》", "克里斯多夫.夏布特"},
                  {"《人的局限性》", "塞缪尔.约翰生"}, 
                  {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
                  {"《大地之上》", "罗欣顿·米斯特里"}, 
                  {"《上学记》", "何兆武"}, 
                  {"《命运》", "蔡崇达"} };
    int n;
    FILE *fp;

    // 计算数组x中元素个数
    n = sizeof(x) / sizeof(x[0]);     
    
    // 以写的方式打开二进制文件data2.dat
    fp = fopen("data2.dat", "wb");
    
    // 如果打开文件失败,输出提示信息并返回 
    if(fp == NULL) {
        printf("fail to open file to write\n");
        return;
    }
    
    // 将结构体数组x中的图书信息以数据块方式写入二进制文件data2.dat
    fwrite(x, sizeof(Book), n, fp);
    
    fclose(fp);
}

void read() {
    Book x[M]; 
    int i, n;
    int number;
    
    FILE *fp;
    
    // 以读的方式打开二进制文件data2.dat
    fp = fopen("data2.dat", "rb");
    
    // 如果打开文件失败,输出提示信息并返回 
    if(fp == NULL) {
        printf("fail to open file to read\n");
        return;
    }
    
    // 从二进制文件data2.dat以数据块方式读取图书信息存储到结构体数组x
    i = 0;
    while(!feof(fp)) {
        number = fread(&x[i], sizeof(Book), 1, fp);
        if(number != 1)
            break;
        i++;
    }

    // 在屏幕上打印输出
    n = i;
    for(i = 0; i < n; ++i)
        printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author);
    
    fclose(fp);
}

task3

// 文件读写操作:以字符、字符串形式读、写

#include <stdio.h>
#include <stdlib.h>

#define N 5
#define M 80

void write();
void read_str();
void read_char();

int main() {
    printf("测试1: 把一组字符信息以字符串方式写入文本文件\n");
    write();

    printf("\n测试2: 从文件以字符串方式读取, 输出到屏幕\n");
    read_str();

    printf("\n测试3: 从文件以单个字符方式读取, 输出到屏幕\n");
    read_char();

    system("pause");
    return 0;
}

void write() {
    char *ptr[N] = { "Working\'s Blues",
                     "Everything Will Flow",
                     "Streets of London",
                     "Perfect Day",
                     "Philadelphia"};
    int i;
    FILE *fp;

    fp = fopen("data3.txt", "w");
    if(!fp) {
        printf("fail to open file to write\n");
        return;
    }

    for(i = 0; i < N; ++i) {
        fputs(ptr[i], fp);
        fputs("\n", fp);
    }
    
    fclose(fp);
}

void read_str() {
    char songs[N][M];
    int i;
    FILE *fp;

    fp = fopen("data3.txt", "r");
    if(!fp) {
        printf("fail to open file to read\n");
        return;
    }

    for(i = 0; i < N; ++i)
        fgets(songs[i], 80, fp);

    for(i = 0; i < N; ++i)
        printf("%d. %s", i+1, songs[i]);
    
    fclose(fp);
}

void read_char() {
    char ch;
    FILE *fp;

    fp = fopen("data3.txt", "r");
    if(!fp) {
        printf("fail to open file to read\n");
        return;
    }

    while(!feof(fp)) {
        ch = fgetc(fp);
        if(ch == EOF)
            break;
        
        putchar(ch);
    }

    fclose(fp);
}

task4

#include <stdio.h>
#include <stdlib.h>

int main(){
    FILE *fp;
    int line_count=0;
    int char_count=0;
    int ch;

    fp=fopen("data4.txt","r");
    if(fp==NULL){
        printf("无法打开文件\n");
        return 1;
    }
    while((ch=fgetc(fp))!=EOF){
        if(ch=='\n')
            line_count++;
        else if(ch!=' '&&ch!='\t'&&ch!='\r')
            char_count++;
    }
    if(char_count>0&&ch!='\n')
        line_count++;

    fclose(fp);

    printf("data4.txt统计结果:\n");
    printf("行数:%d\n",line_count);
    printf("字符数(不计空白符):%d\n",char_count);

    system("pause");
    return 0;
}

task5

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define N 10

typedef struct {
    long id;            // 准考证号
    char name[20];      // 姓名
    float objective;    // 客观题得分
    float subjective;   // 操作题得分
    float sum;          // 总分
    char result[10];    // 考试结果
} STU;

// 函数声明
void read(STU st[], int n);
void write(STU st[], int n);
void output(STU st[], int n);
int process(STU st[], int n, STU st_pass[]);

int main() {
    STU stu[N], stu_pass[N];
    int cnt;
    double pass_rate;

    printf("从文件读入%d个考生信息...\n", N);
    read(stu, N);

    printf("\n对考生成绩进行统计...\n");
    cnt = process(stu, N, stu_pass);

    printf("\n通过考试的名单:\n");
    output(stu, N);   // 输出所有考生完整信息到屏幕
    write(stu, N);    // 输出考试通过的考生信息到文件

    pass_rate = 1.0 * cnt / N;
    printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);

    system("pause");
    return 0;
}

// 把所有考生完整信息输出到屏幕上
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void output(STU st[], int n) {
    int i;
    
    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", st[i].id, st[i].name, st[i].objective, st[i].subjective, st[i].sum, st[i].result);
}

// 从文本文件examinee.txt读入考生信息:准考证号,姓名,客观题得分,操作题得分
void read(STU st[], int n) {
    int i;
    FILE *fin;

    fin = fopen("examinee.txt", "r");
    if (!fin) {
        printf("fail to open file\n");
        return;
    }

    while (!feof(fin)) {
        for (i = 0; i < n; i++)
            fscanf(fin, "%ld %s %f %f", &st[i].id, st[i].name, &st[i].objective, &st[i].subjective);
    }

    fclose(fin);
}

// 把通过考试的考生完整信息写入文件list_pass.txt
// 准考证号,姓名,客观题得分,操作题得分,总分,结果
void write(STU s[], int n) {
    FILE *fout;
    int i;
    fout=fopen("list_pass.txt","w");
    if(!fout){
        printf("fail to open file for writing\n");
            return;
    }
    for(i=0;i<n;i++)
        fprintf(fout,"%ld %s %.2f %.2f %.2f %s\n",s[i].id,s[i].name,s[i].objective,s[i].subjective,s[i].sum,s[i].result);
    fclose(fout);
}

int process(STU st[], int n, STU st_pass[]) {
    int i,j=0;
    for(i=0;i<n;i++){
        st[i].sum=st[i].objective+st[i].subjective;
        if(st[i].sum>=60){
            strcpy(st[i].result,"通过");
            st_pass[j]=st[i];
            j++;
        }
        else
            strcpy(st[i].result,"未通过");
    }
    return j;
}

task6

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h> 
#define N 80

int main() {
    char info[N][80];
    int flag[N] = {0};
    int i, j, count = 0;
    int lucky_index;
    char filename[50]; 
    char lucky_info[5][80] = {0};
    FILE *fp, *fw;

    fp = fopen("list.txt", "r");
    if (!fp) {
        printf("fail to open file to read\n");
        return 1;
    }
    for (i = 0; i < N; ++i)
        fgets(info[i], 80, fp);
    fclose(fp);

    srand(time(NULL));
    
    while (count < 5) {
        lucky_index = rand() % N;
        if (!flag[lucky_index]) { 
            strcpy(lucky_info[count], info[lucky_index]); 
            flag[lucky_index] = 1;
            count++;
        }
    }

    printf("中奖名单:\n");
    for (i = 0; i < 5; i++) {
        printf("%s", lucky_info[i]);
    }

    printf("\n请输入要写入的文件名:");
    scanf("%s", filename); 

    fw = fopen(filename, "w");
    if (!fw) {
        printf("fail to open file to write\n");
        return 1;
    }

    fprintf(fw, "中奖名单:\n");
    for (i = 0; i < 5; i++) {
        fputs(lucky_info[i], fw); 
    }
    fclose(fw);
    printf("中奖名单已写入文件:%s\n", filename);

    system("pause");
    return 0;
}

 

posted @ 2025-06-09 10:06  乐一乐路过  阅读(12)  评论(0)    收藏  举报