实验内容

实验任务1

  • 源代码
// 文件读写操作:格式化读、写文本文件
#include <stdio.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();
    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;
    
    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(fscanf(fp, "%s%s", x[i].name, x[i].author) != EOF) 
        ++i;
    
    // 将图书信息打印输出到屏幕上
    n = i;
    for(i = 0; i < n; ++i)
        printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author);
    
    fclose(fp);
}
  • 结果截图
    image

实验任务2

  • 源代码
#include <stdio.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();
    return 0;
}
void write() {
    Book x[] = { {"《雕塑家》", "斯科特.麦克劳德"},
            {"《灯塔》", "克里斯多夫.夏布特"},
            {"《人的局限性》", "塞缪尔.约翰生"},
            {"《永不停步:玛格丽特.阿特伍德传》", "罗斯玛丽.沙利文"},
            {"《大地之上》", "罗欣顿·米斯特里"},
            {"《上学记》", "何兆武"},
            {"《命运》", "蔡崇达"} };
    int n, i;
    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;
    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(fread(&x[i], sizeof(Book), 1, fp) == 1)
    ++i;
    // 在屏幕上打印输出
    n = i;
    for(i = 0; i < n; ++i)
    printf("%d. %-40s%-20s\n", i+1, x[i].name, x[i].author);
    fclose(fp);
}
  • 结果截图
    image

实验任务3

  • 源代码
#include <stdio.h>
#define N 100
#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();
    return 0;
}
void write() {
char *ptr[] = { "Working\'s Blues",
            "Everything Will Flow",
            "Streets of London",
            "Perfect Day",
            "Philadelphia"};
    int i, n;
    FILE *fp;
    fp = fopen("data3.txt", "w");
    if(fp == NULL) {
        printf("fail to open file to write\n");
        return;
    }
    n = sizeof(ptr)/sizeof(ptr[0]);
    for(i = 0; i < n; ++i) {
        fputs(ptr[i], fp);
        fputs("\n", fp);
    }
    fclose(fp);
}
void read_str() {
    char songs[N][M];
    int i, n;
    FILE *fp;
    fp = fopen("data3.txt", "r");
    if(fp == NULL) {
        printf("fail to open file to read\n");
    return;
    }
    i = 0;
    while(i < N && (fgets(songs[i], M, fp) != NULL))
        ++i;
    n = i;
    for(i = 0; i < n; ++i)
        printf("%d. %s", i+1, songs[i]);
    fclose(fp);
}
void read_char() {
    int ch;
    FILE *fp;
    fp = fopen("data3.txt", "r");
    if(fp == NULL) {
        printf("fail to open file to read\n");
    return;
    }
    while((ch = fgetc(fp)) != EOF)
        putchar(ch);
    fclose(fp);
}
  • 结果截图
    image
    思考:1. ' 是转义字符,转义就是 ' ,需要\才行。
    2.防止读取的行数超过数组容量 100 行,导致写入 songs[100] 及后面的内存,防止缓冲区溢出。

实验任务4

  • 源代码
#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE* pf = fopen("data4.txt", "r");
    if(pf == NULL) {
        printf("fail open file\n");
        return 1;
    }

    int ch;
    int lines = 0;
    int chars = 0;
    int tem = 0;

    while((ch = fgetc(pf)) != EOF) {
        if(ch == '\n') {
            lines++;
        }
        else if(ch != ' ' && ch != '\t' && ch != '\r') 
        {
            chars++;
            tem = 1;
        }
    }

    if(tem) {
        lines++;
    }

    printf("lines: %d\n", lines);
    printf("chars: %d\n", chars);

    fclose(pf);
    return 0;
}
  • 结果截图
    image

实验任务5

  • 源代码
#include <stdio.h>
#include <string.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);
    printf("\n通过考试的名单写入文件: 已完成!\n");
    write(stu_pass, cnt);
    pass_rate = 1.0 * cnt / N;
    printf("\n本次等级考试通过率: %.2f%%\n", pass_rate*100);
    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;
    }
    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);
}

// 对考生信息进行处理:计算每位考生考试总分、结果;统计并返回通过考试的人数
int process(STU st[], int n, STU st_pass[]) {
    int i=0;
    int j=0;
    while(i<n)
    {
        st[i].sum=st[i].objective+st[i].subjective;
        if(st[i].sum>=60)
        {
            strcpy(st[i].result,"通过");
            st_pass[j++]=st[i];
        }
        else
        {
            strcpy(st[i].result,"不通过");
        }
        i++;
    }
    return j;
}

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

实验任务6

  • 源代码
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

int main()
{
    FILE *fp;
    char s[80][256];
    int n = 0, i, j, t, m[5], flag[80] = {0};
    char name[256], day[64];
    time_t now;
    struct tm *p;

    fp = fopen("list.txt", "r");
    if (!fp) { printf("无法打开文件\n"); return 1; }
    while (n < 80 && fgets(s[n], 256, fp)) {
        int len = strlen(s[n]);
        while (len > 0 && (s[n][len-1]=='\n' || s[n][len-1]=='\r')) s[n][--len] = 0;
        n++;
    }
    fclose(fp);

    srand((unsigned)time(NULL));
    for (i = 0; i < 5; i++) {
        do { t = rand() % 80; } while (flag[t]);
        flag[t] = 1;
        m[i] = t;
    }
    for (i = 0; i < 4; i++)
        for (j = i+1; j < 5; j++)
            if (strcmp(s[m[i]], s[m[j]]) > 0) {
                t = m[i]; m[i] = m[j]; m[j] = t;
            }

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

    printf("请输入输出文件名: ");
    scanf("%s", name);

    fp = fopen(name, "w");
    if (fp) {
        fprintf(fp, "===== 中奖名单 =====\n");
        for (i = 0; i < 5; i++) fprintf(fp, "%s\n", s[m[i]]);
        fprintf(fp, "====================\n");
        fclose(fp);
        printf("已写入 %s\n", name);
    }

    now = time(NULL);
    p = localtime(&now);
    sprintf(day, "%d%02d%02d.txt", p->tm_year+1900, p->tm_mon+1, p->tm_mday);

    fp = fopen(day, "w");
    if (fp) {
        fprintf(fp, "===== 中奖名单 =====\n");
        for (i = 0; i < 5; i++) fprintf(fp, "%s\n", s[m[i]]);
        fprintf(fp, "====================\n");
        fclose(fp);
        printf("已写入 %s\n", day);
    }

    return 0;
}

  • 结果截图

image

posted on 2026-06-23 20:26  suuus  阅读(9)  评论(0)    收藏  举报