标签:class 查看 include fclose txt 美化 内存 成绩 com
你现在拥有一个数组,数组中储存着总共10个人的姓名字符串
你需要为每个人创建一个txt文件,以他们的名字命名。
#include<stdio.h>
int main() {
FILE* fp;
char name[10][20] = { "宋林涛","黄力强","陈汉煜","张旭","李龙生","蔡泓顺","郝冰冰","茅志雄","雷雯雯","朱仕岳" };
int i;
for (i = 0;i < 10;i++) {
sprintf(name[i], "%s.txt", name[i]);
fp = fopen(name[i], "w");
if (fp == NULL) {
break;
}
fclose(fp);
}
return 0;
}
在任务一的基础上,这次不仅仅要创建txt文件,还需要往文件中写入
每个人的学号,性别,班级,线代成绩
#include<stdio.h>
struct stu {
char id[100];
char name[30];
char grade[10];
char sex[10];
char Class[10];
}s1[100];
int main() {
FILE* fp;
char name[10][20] = { "宋林涛","黄力强","陈汉煜","张旭","李龙生","蔡泓顺","郝冰冰","茅志雄","雷雯雯","朱仕岳" };
int i;
for (i = 0;i < 10;i++) {
sprintf(name[i], "%s.txt", name[i]);
fp = fopen(name[i], "w+");
scanf("%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
fprintf(fp, "%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
fclose(fp);
}
return 0;
}
在任务二生成的文件中,将每个人的信息再重新读取出来,放入数组中。
[要求使用结构体数组,结构体需要包含姓名,学号,性别,班级,线代成绩5个属性]
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct stu {
char id[100];
char name[30];
char grade[10];
char sex[10];
char Class[10];
}s1[100];
int main() {
FILE* fp;
char name[100][20] = { "宋林涛","黄力强","陈汉煜","张旭","李龙生","蔡泓顺","郝冰冰","茅志雄","雷雯雯","朱仕岳" };
char name2[100][20] = { "宋林涛","黄力强","陈汉煜","张旭","李龙生","蔡泓顺","郝冰冰","茅志雄","雷雯雯","朱仕岳" };
int i;
for (i = 0;i < 10;i++) {
sprintf(name[i], "%s.txt", name[i]);
//fp = fopen(name[i], "w+");
//scanf("%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
//fprintf(fp, "%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
//fclose(fp);
}
system("cls");
for (i = 0;i < 10;i++) {
if ((fp = fopen(name[i], "r")) == NULL) {
printf("File open error");
exit(0);
}
while (!feof(fp)) {
fscanf(fp, "%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
}
strcpy(s1[i].name, name2[i]);
printf("%s\t%s\t%s\t%s\t%s\n", s1[i].name,s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
}
fclose(fp);
return 0;
}
试着使用一下system("cls"),system("pause")这两个命令,看看这两个
命令能不能对你的程序起到一些美化的作用。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
struct stu {
char id[100];
char name[30];
char grade[10];
char sex[10];
char Class[10];
}s1[100];
int main() {
system("color E1");
FILE* fp;
char name[100][20] = { "宋林涛","黄力强","陈汉煜","张旭","李龙生","蔡泓顺","郝冰冰","茅志雄","雷雯雯","朱仕岳" };
char name2[100][20] = { "宋林涛","黄力强","陈汉煜","张旭","李龙生","蔡泓顺","郝冰冰","茅志雄","雷雯雯","朱仕岳" };
int i;
printf(" ┏━━━━━━━━━━━━━━━━━━━━━━━┓ \n");
printf(" ┃ 学生管理系统 ┃ \n");
printf(" ┣━━━━━━━━━━━━━━━━━━━━━━━┫ \n");
printf(" ┃ 欢迎使用 ┃ \n");
printf(" ┃ 按任意键继续 ┃ \n");
printf(" ┗━━━━━━━━━━━━━━━━━━━━━━━┛ \n");
system("pause");
system("cls");
printf("请依次输入\n");
printf("学号 性别 班级 线代成绩\n");
for (i = 0;i < 10;i++) {
sprintf(name[i], "%s.txt", name[i]);
fp = fopen(name[i], "w+");
scanf("%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
fprintf(fp, "%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
fclose(fp);
}
system("pause");
system("cls");
printf("查看学生成绩\n");
system("pause");
system("cls");
printf("学生成绩如下\n");
for (i = 0;i < 10;i++) {
if ((fp = fopen(name[i], "r")) == NULL) {
printf("File open error");
exit(0);
}
while (!feof(fp)) {
fscanf(fp, "%s %s %s %s", s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
}
strcpy(s1[i].name, name2[i]);
printf("%s\t%s\t%s\t%s\t%s\n", s1[i].name,s1[i].id, s1[i].sex, s1[i].Class, s1[i].grade);
}
system("pause");
system("cls");
printf(" ┏━━━━━━━━━━━━━━━━━━━━━━━┓ \n");
printf(" ┃ 学生管理系统 ┃ \n");
printf(" ┣━━━━━━━━━━━━━━━━━━━━━━━┫ \n");
printf(" ┃ 感谢使用 ┃ \n");
printf(" ┗━━━━━━━━━━━━━━━━━━━━━━━┛ \n");
Sleep(3000);
fclose(fp);
return 0;
}
要引入头文件include<windows.h>
system("cls");清屏
system("pause");暂停程序,按任意键继续
system ("color ab")改变界面的背景色和字体颜色,前面的为背景色,后面的为字体颜色。
system(“ shutdown -s -t ”) 可以让电脑关机
注意到打开文件后一定要关闭文件,否则会报错,进而了解到文件打开后,可能有一些操作被缓冲在内存中,若不正常关闭,缓冲在内存中的数据就不能真正写入到文件中,可能造成数据丢失。
标签:class 查看 include fclose txt 美化 内存 成绩 com
原文地址:https://www.cnblogs.com/song0820/p/12055506.html