首页 > 编程语言 > 详细

C语言文件

时间:2019-12-21 22:53:41      阅读:107      评论:0      收藏:0      [点我收藏+]

任务一:

你现在拥有一个数组,数组中储存着总共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使程序看起来更加明朗,更加美观。

C语言文件

原文:https://www.cnblogs.com/Tvoimvyan/p/12078210.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!