C语言文件

任务一:

你现在拥有一个数组,数组中储存着总共10个人的姓名字符串
你需要为每个人创建一个txt文件,以他们的名字命名。
例如: 生成 笨笨.txt文件

代码展示:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main(){
    FILE *fp;
    int i;
    char name_students[10][40]={ "杨涛", "蔡锦涛", "李永福", "吴执涛", "吴绍杰", "黄家雯", "黄东", "林洁颖", "钟雄飞", "傻子涛" };
    
    for( i=0; i<10; i++) {
        strcat( name_students[i],".txt");
        if( ( fp=fopen(name_students[i],"w"))==NULL ) {
            printf("文件无法打开!\n");
            exit( 0 );
        }
        if( fclose(fp) ) {
            printf("文件无法关闭!\n");
            exit( 0 );
        }
    }
    
    return 0;
}

结果展示:

 

任务二:

在任务一的基础上,这次不仅仅要创建txt文件,还需要往文件中写入
每个人的学号,性别,班级,线代成绩

代码展示

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student {
    char name[10];
    char number_stu[25];
    char sex[10];
    char Class[10];
    char score[5];
};
int main(){
    FILE *fp;
    int i;
    char name_students[10][40]={ "杨涛", "蔡锦涛", "李永福", "吴执涛", "吴绍杰", "黄家雯", "黄东", "林洁颖", "钟雄飞", "傻子涛" };
    struct student data[10];
   
    printf("依次输入学号,性别,班级和线代成绩\n");
    printf("姓名:    学号:      性别:    班级:    线代成绩:     \n");
   
    for( i=0; i<10; i++) {
        printf("%s",name_students[i]);
       
        strcat( name_students[i],".txt");
        if( ( fp=fopen(name_students[i],"w"))==NULL ) {
            printf("文件无法打开!\n");
            exit( 0 );
        }
        scanf("%s %s %s %s", data[i].number_stu, data[i].sex, data[i].Class, data[i].score);
        fprintf(fp, "%s %s %s %s",data[i].number_stu, data[i].sex, data[i].Class, data[i].score);
       
        if( fclose(fp) ) {
            printf("文件无法关闭!\n");
            exit( 0 );
        }
    }
   
    return 0;
}

输入数据:

结果展示:

 

 

 

任务三:

在任务二生成的文件中,将每个人的信息再重新读取出来,放入数组中。
[要求使用结构体数组,结构体需要包含姓名,学号,性别,班级,线代成绩5个属性]

 

代码展示:

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct student {
    char name[10];
    char number_stu[25];
    char sex[10];
    char Class[10];
    char score[5];
};
int main() {
    FILE *fp;
    int i;
    struct student data[10];
    char name_students[10][20] = { "ŐĹĐń","łÂź@","ÖěĘËÔŔ","Ă©ÖľĐŰ","ŔîÁúÉú","ÎâÖ´ĚÎ","şéŔńÇż","ŔîĐŔĐŔ","±ż±ż","ËÎËĽĆÂ" };
    for (i = 0; i < 10; i++) {
        strcpy(data[i].name, name_students[i]);
        strcat(name_students[i], ".txt");
        if ((fp = fopen(name_students[i], "r"))==NULL) {
            printf("ÎÄĽţ´ňżŞĘ§°ÜŁˇ\n");
            exit(0);
        }
        fscanf(fp, "%s %s %s %s", data[i].number_stu, data[i].sex, data[i].Class, data[i].score);
        if (fclose(fp)) {
           printf("ÎÄĽţąŘ±Őʧ°ÜŁˇ\n");
           exit(0);
        }
    }
    printf("ĐŐĂűŁş     ѧşĹŁş       ĐԱ𣺠    °ŕĽ¶Łş     Ďß´úłÉĽ¨Łş     \n");
    for (i = 0; i < 10; i++) {
        printf("%-11s%-13s%-11s%-11s%s\n", data[i].name, data[i].number_stu, data[i].sex, data[i].Class, data[i].score);
    }
    return 0;
}

 

任务四:

试着使用一下system("cls"),system("pause")这两个命令,看看这两个
命令能不能对你的程序起到一些美化的作用。
拓展:上网搜索类似的其他函数,使用并解释他们的作用。
【想找几个是几个】

代码展示:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct student {
    char name[10];
    char number_stu[25];
    char sex[10];
    char Class[10];
    char score[5];
};
int main(){
    FILE *fp;
    int i;
    char name_students[10][40]={ "ŃîĚÎ", "˛Ě˝őĚÎ", "ŔîÓŔ¸Ł", "ÎâÖ´ĚÎ", "ÎâÉÜ˝Ü", "»ĆĽŇö©", "»Ć¶«", "ÁÖ˝ŕÓ±", "ÖÓĐŰ·É", "ɵ×ÓĚÎ" };
    struct student data[10];
    system("color F5");
    printf("»¶Ó­ĘąÓĂĽňŇ×°ćłÉĽ¨ÂĽČëϵͳ\n");
    system("pause");
    system("cls");
    
    
    printf("ŇŔ´ÎĘäČëѧşĹŁ¬ĐԱ𣬰༶şÍĎß´úłÉĽ¨\n");
    printf("ĐŐĂűŁş    ѧşĹŁş      ĐԱ𣺠   °ŕĽ¶Łş    Ďß´úłÉĽ¨Łş     \n");
    
    for( i=0; i<10; i++) {
        printf("%s",name_students[i]);
        
        strcat( name_students[i],".txt");
        if( ( fp=fopen(name_students[i],"w"))==NULL ) {
            printf("ÎÄĽţÎŢ·¨´ňżŞŁˇ\n");
            exit( 0 );
        }
        scanf("%s %s %s %s", data[i].number_stu, data[i].sex, data[i].Class, data[i].score);
        fprintf(fp, "%s %s %s %s",data[i].number_stu, data[i].sex, data[i].Class, data[i].score);
        
        if( fclose(fp) ) {
            printf("ÎÄĽţÎŢ·¨ąŘ±ŐŁˇ\n");
            exit( 0 );
        }
    }
    
    return 0;
}

 

结果展示:

 

 

 

 

总结:文件的使用让我对C语言编程中程序的进行有了更深的理解,system函数中的cls、pause、color使程序看起来更加明朗,更加美观。

posted @ 2019-12-21 22:29  Tvivanomy(王林涛)  阅读(329)  评论(0编辑  收藏  举报