c语言文件

任务一

你现在拥有一个数组,数组中储存着总共10个人的姓名字符串。
从数组的名字中为每一个人创建txt文件。

#include <stdio.h>
int main()
{
    char name_ALL[10][20] = {"AA","BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ"};
    FILE *fp[10];
    int i;
    char file_path[100];
    
    for (i=0; i<10; i++)
    {
        sprintf(file_path, "/Users/wuzhitao/Documents/CLanguage/FILE-demo/%s.txt", name_ALL[i]);
        if ((fp[i] = fopen(file_path, "w")) == NULL)
        {
            printf("File open error
");
            exit(0);
        }
    }
    for (i=0; i<10; i++)
    {
        if (fclose(fp[i]))
        {
            printf("Can not close the file
");
            exit(0);
        }
    }
    return 0;
}

运行结果:

任务二

往文件中写入每个人的学号,性别,班级,线代成绩。

#include <stdio.h>
int main()
{
    char name_ALL[10][20] = {"AA","BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ"};
    FILE *fp[10];
    int i;
    char file_path[100];
    
    for (i=0; i<10; i++)
    {
        sprintf(file_path, "/Users/wuzhitao/Documents/CLanguage/FILE-demo/%s.txt", name_ALL[i]);
        if ((fp[i] = fopen(file_path, "a")) == NULL)
        {
            printf("File open error
");
            exit(0);
        }
    }
    
    char student_number[80], gender[80], class[80];
    int score;
    for (i=0; i<10; i++)
    {
        printf("请输入%s的学号,性别,班级,线代成绩
", name_ALL[i]);
        scanf("%s %s %s %d", student_number, gender, class, &score);
        fprintf(fp[i], "%s %s %s %d", student_number, gender, class, score);
    }
    for (i=0; i<10; i++)
    {
        if (fclose(fp[i]))
        {
            printf("Can not close the file
");
            exit(0);
        }
    }
    return 0;
}

运行截图:


txt文件:

任务三

在任务二生成的文件中,将每个人的信息再重新读取出来,放入数组中。

#include <stdio.h>
#include <string.h>
struct students{
    char name[80], student_number[80], gender[80], class[80];
    int score;
};
int main()
{
    struct students student[10];
    char name_ALL[10][20] = {"AA","BB", "CC", "DD", "EE", "FF", "GG", "HH", "II", "JJ"};
    FILE *fp[10];
    int i;
    char file_path[100];
    
    for (i=0; i<10; i++)
    {
        sprintf(file_path, "/Users/wuzhitao/Documents/CLanguage/FILE-demo/%s.txt", name_ALL[i]);
        if ((fp[i] = fopen(file_path, "r")) == NULL)
        {
            printf("File open error
");
            exit(0);
        }
    }
    for (i=0; i<10; i++)
    {
        strcpy(student[i].name, name_ALL[i]);
        fscanf(fp[i], "%s %s %s %d", student[i].student_number, student[i].gender, student[i].class, &student[i].score);
    }
    for (i=0; i<10; i++)
    {
        if (fclose(fp[i]))
        {
            printf("Can not close the file
");
            exit(0);
        }
    }
    printf("姓名  学号     性别      班级    线代成绩
");
    for (i=0; i<10; i++)
    {
        printf("%-5s%-9s%-9s%-7s%-5d
", student[i].name, student[i].student_number, student[i].gender, student[i].class, student[i].score);
    }
    return 0;
}

运行截图:

任务四

    system("pause");//暂停,按任意键继续
    system("cls");//清屏
    system("title 学生信息打印");//更改cmd窗口的标题
    system("color 70");//更改cmd窗口的背景色与前景色

颜色属性由两个十六进制数字指定:第一个对应于背景,第二个对应于前景。
每个数字可以为以下任何值:
0 = 黑色 8 = 灰色
1 = 蓝色 9 = 淡蓝色
2 = 绿色 A = 淡绿色
3 = 浅绿色 B = 淡浅绿色
4 = 红色 C = 淡红色
5 = 紫色 D = 淡紫色
6 = 黄色 E = 淡黄色
7 = 白色 F = 亮白色
实现效果:

注:发现system语句只能在Windows环境下才能使用,在其他系统就...

原文地址:https://www.cnblogs.com/wzt392217419/p/12043906.html