zoukankan      html  css  js  c++  java
  • 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环境下才能使用,在其他系统就...

  • 相关阅读:
    Go语言开发Windows应用
    go 调用windows dll 的方法
    thinkPHP5 命名空间别名
    thinkPHP5 类库包注册
    thinkphp5 默认配置代码
    edusoho twig 引入文件功能
    edusoho 查找网址对应的控制器和模板页面
    启动Nginx 出现 nginx: [emerg] unknown directive "锘?user" 错误
    eduSOHO 首页模板 全部课程模块代码
    twig 模板控制器对应列表
  • 原文地址:https://www.cnblogs.com/wzt392217419/p/12043906.html
Copyright © 2011-2022 走看看