程序设计题
从键盘输入两个学生数据,写入一个文件中,再读出这两个学生的数据显示在屏幕上。
struct student { char name[20]; int age; }; int main() { struct student s[2] = {0}; FILE* pf; //输入数据 for (int i = 0; i < 2; i++) { printf("输入第%d个学生信息(姓名 年龄): ", i + 1); if (scanf("%19s %d", s[i].name, &s[i].age) != 2) { printf("输入格式错误!\n"); return 1; } } pf = fopen("inf.txt", "w"); if (pf==NULL) { perror("fopen"); return 1; } for (int i = 0; i < 2; i++) { fprintf(pf, "%s %d\n", s[i].name, s[i].age); //信息输入 // 添加换行符 } fclose(pf); //读取并显示 pf = fopen("inf.txt", "r"); if (pf == NULL) { perror("fopen"); return 1; }
printf("\n文件内容:\n"); struct student temp; for (int i = 0; i < 2; i++) { if (fscanf(pf, "%19s %d", temp.name, &temp.age) == 2) { printf("学生%d: %s %d\n", i + 1, temp.name, temp.age); } } fclose(pf); pf = NULL; return 0; }
struct student { int id; char name[20]; float score; }stu1,stu2; int main(int argc, char* argv[]) { // 从键盘输入两个学生数据,写入一个文件中,再读出这两个学生的数据显示在屏幕上。 FILE* source; printf("请输入第一个学生数据:\n"); scanf("%d %s %f", &stu1.id, stu1.name, &stu1.score); printf("请输入第二个学生数据:\n"); scanf("%d %s %f", &stu2.id, stu2.name, &stu2.score); // 打开源文件 source = fopen("example.txt", "w"); if (source == NULL) { printf("无法打开源文件: %s\n", argv[1]); return 1; } // 写入文件 fprintf(source, "id=%d ## name=%s ## score=%f \n", stu1.id, stu1.name, stu1.score); fprintf(source, "id=%d ## name=%s ## score=%f \n", stu2.id, stu2.name, stu2.score); // 关闭文件 fclose(source); // 重新打开文件进行读取 source = fopen("example.txt", "r"); if (source == NULL) { printf("无法读取文件 example.txt\n"); return 1; } // 读取数据 char buffer[50]; struct student temp1, temp2; fgets(buffer, sizeof(buffer), source); sscanf(buffer, "id=%d ## name=%s ## score=%f \n", &temp1.id, temp1.name, &temp1.score); fgets(buffer, sizeof(buffer), source); sscanf(buffer, "id=%d ## name=%s ## score=%f", &temp2.id, temp2.name, &temp2.score); fclose(source); // 显示读取的数据 printf("\n读取的学生数据:\n"); printf("ID: %d, Name: %s, Score: %.2f\n", temp1.id, temp1.name, temp1.score); printf("ID: %d, Name: %s, Score: %.2f\n", temp2.id, temp2.name, temp2.score); return 0; }
FILE *fp; fp=fopen("test.txt","w+"); char buff[70]; int x,y; scanf("%d%d",&x,&y); fprintf(fp,"第一个学生的数据为:%d\n",x); fprintf(fp,"第二个学生的数据为:%d\n",y); fgets(buff,70,(FILE*)fp); printf("%s",buff);
#include #include #include typedef struct { char name[20]; int score; }stu; int main(){ FILE *fp; char c; stu student[2]; stu r_student[2]; fp=fopen("1.txt","r+"); strcpy(student[0].name,"张三"); strcpy(student[1].name,"李四"); student[0].score=100; student[1].score=10; fwrite(&student[0],sizeof(student[0]),1,fp); fwrite(&student[1],sizeof(student[1]),1,fp); fseek(fp,0,0); fread(r_student,sizeof(r_student),1,fp); fclose(fp); printf("name = %s, grade = %d\n", r_student[0].name, r_student[0].score); printf("name = %s, grade = %d\n", r_student[1].name, r_student[1].score); return 0; }
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct { char name[20]; int score; }stu; int main(){ FILE *fp; char c; stu student[2]; stu r_student[2]; fp=fopen("1.txt","r+"); strcpy(student[0].name,"张三"); strcpy(student[1].name,"李四"); student[0].score=100; student[1].score=10; fwrite(&student[0],sizeof(student[0]),1,fp); fwrite(&student[1],sizeof(student[1]),1,fp); fseek(fp,0,0); fread(r_student,sizeof(r_student),1,fp); fclose(fp); printf("name = %s, grade = %d\n", r_student[0].name, r_student[0].score); printf("name = %s, grade = %d\n", r_student[1].name, r_student[1].score); return 0; }
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> #include<stdlib.h> #include<string.h> struct stu { char name[20]; float score; }student; int main(void) {
FILE* fp =fopen("D:\\OneDrive\\桌面\\test.txt", "w"); char ch[15]; if (fp == NULL) { puts("打开文件失败"); exit(1); // 非0值表示异常退出 } for (int i = 0; i < 2; i++) { printf("请输入第%d个学生的数据\n", i + 1); scanf("%s%f", student.name, &student.score); fwrite(&student, sizeof(struct stu), 1, fp); } fclose(fp);
fp = fopen("D:\\OneDrive\\桌面\\test.txt", "r"); while (fread(&student, sizeof(struct stu), 1, fp)) { printf("name=%s score=%.2f\n", student.name, student.score); } printf("\nEND!!!\n"); fclose(fp);
return 0; }
#include #include typedef struct student{ int num; int score; }Stu; Stu stu1={1001,100},stu2={1002,99}; int main(){ FILE *fp; if((fp=fopen("abc.docx","w+"))==NULL){ printf("error"); exit(0); } fwrite(&stu1,sizeof(Stu),1,fp); fwrite(&stu2,sizeof(Stu),1,fp); rewind(fp); Stu stu_new1,stu_new2; fread(&stu_new1,sizeof(Stu),1,fp); fread(&stu_new2,sizeof(Stu),1,fp); printf("%d\t%d",stu_new1.num,stu_new2); fclose(fp); }
#include <stdio.h> #include <string.h>
typedef struct { char name[20]; int grade; }Stu;
int main() { Stu student[2]; strcpy(student[0].name, "张三"); strcpy(student[1].name, "李四"); student[0].grade = 10; student[1].grade = 99; // 写入 FILE* fp = fopen("write_test.txt", "w+"); if (fp == NULL) { printf("打开文件失败"); getchar(); return 1; } fwrite(&student[0], sizeof(student[0]), 1, fp); fwrite(&student[1], sizeof(student[1]), 1, fp); fclose(fp);
// 读文件 fp = fopen("write_test.txt", "r+"); if (fp == NULL) { printf("打开文件失败"); getchar(); return 1; } Stu r_student[2]; fread(r_student, sizeof(r_student) * 2, 1, fp); fclose(fp);
printf("name = %s, grade = %d\n", r_student[0].name, r_student[0].grade); printf("name = %s, grade = %d\n", r_student[1].name, r_student[1].grade);
#include <stdio.h> struct student{ char name[100]; float score; }person; int main() { FILE *fp; fp = fopen("1600.dat", "wb"); for(int i=0; i<2; i++) { scanf("%s %f", person.name, &person.score); fwrite(&person, sizeof(struct student), 1, fp); } fclose(fp); fp = fopen("1600.dat", "rb"); while(fread(&person, sizeof(struct student), 1, fp)) { printf("name=%s score=%f\n", person.name, person.score); } return 0; }
#include <stdio.h>
struct Student
{
char name[20];
int age;
};
int main()
struct Student stu[2];
for (int i = 0; i < 2; i++)
printf("input student%d's name:", i + 1);
gets(stu[i].name);
printf("input student%d's age:", i + 1);
scanf("%d", &stu[i].age);
getchar();
}
FILE *fp;
if ((fp = fopen("student", "w")) == NULL)
printf("打开文件失败\n");
return 0;
fwrite(stu, sizeof(struct Student), 2, fp);
fclose(fp);
if ((fp = fopen("student", "r")) == NULL)
struct Student stu2[2];
fread(stu2, sizeof(struct Student), 2, fp);
printf("student%d's name:%s\n", i + 1, stu2[i].name);
printf("student%d's age:%d\n", i + 1, stu2[i].age);
#include<stdio.h> #include<stdlib.h> struct stu { int num; char name[20]; double score; }a[2], b[2]; int main() { FILE *fp = fopen("d:/string.txt", "wb"); if(fp == NULL) { printf("文件打开错误,程序退出"); exit(-1); } for(int i = 0; i < 2; i++) { printf("请输入第%d个学生的学号姓名和成绩:\n", i+1); scanf("%d%s%lf", &a[i].num, a[i].name, &a[i].score); } fwrite(a, sizeof(stu), 2, fp); fclose(fp); fp = fopen("d:/string.txt", "rb"); if(fp == NULL) { printf("文件读入错误,程序退出"); exit(-1); } fread(b, sizeof(stu), 2, fp); for(int i = 0; i < 2; ++i) { printf("学号:%d,姓名:%s,成绩:%.2lf\n", a[i].num, a[i].name, a[i].score); } fclose(fp); fp = NULL; return 0; }
#include<stdio.h> struct Student{ char name[20]; int age; float score; }; int main(){ struct Student stu1, stu2; FILE *fp; printf("请输入第一个学生的姓名、年龄和成绩:"); scanf("%s %d %f", stu1.name, &stu1.age, &stu1.score); printf("请输入第二个学生的姓名、年龄和成绩:"); scanf("%s %d %f", stu2.name, &stu2.age, &stu2.score); fp = fopen("student.txt", "w"); if(fp == NULL){ printf("Failed to open the file:student.txt\n"); return 1; } fprintf(fp, "%s %d %f\n",stu1.name, stu1.age, stu1.score); fprintf(fp, "%s %d %f\n",stu2.name, stu2.age, stu2.score); fclose(fp); fp = fopen("student.txt", "r"); if(fp == NULL){ printf("Failed to open the file:student.txt\n"); return 1; } fscanf(fp, "%s %d %f\n", stu1.name, &stu1.age, &stu1.score); fscanf(fp, "%s %d %f\n", stu2.name, &stu2.age, &stu2.score); printf("第一个学生的信息为:%s %d %f\n",stu1.name, stu1.age, stu1.score); printf("第二个学生的信息为:%s %d %f\n",stu2.name, stu2.age, stu2.score); return 0; }
#include <stdio.h> typedef struct { char name[20]; int age; float score; } Student; void inputStudentData(Student *student) { printf("请输入学生的姓名:"); scanf("%s", student->name); printf("请输入学生的年龄:"); scanf("%d", &(student->age)); printf("请输入学生的成绩:"); scanf("%f", &(student->score)); } void writeStudentDataToFile(const Student *student, FILE *file) { fwrite(student, sizeof(Student), 1, file); } void readStudentDataFromFile(FILE *file) { printf("从文件中读取的学生数据如下:\n"); Student student; while (fread(&student, sizeof(Student), 1, file) > 0) { printf("姓名:%s\t年龄:%d\t成绩:%.2f\n", student.name, student.age, student.score); } } int main() { FILE *file = fopen("students.dat", "wb"); if (file == NULL) { printf("无法打开文件。\n"); return 1; } int numStudents; printf("请输入学生的数量:"); scanf("%d", &numStudents); for (int i = 0; i < numStudents; i++) { Student student; printf("请输入第%d个学生的数据:\n", i + 1); inputStudentData(&student); writeStudentDataToFile(&student, file); } fclose(file); file = fopen("students.dat", "rb"); if (file == NULL) { printf("无法打开文件。\n"); return 1; } readStudentDataFromFile(file); fclose(file); return 0; }
#include<stdio...
登录后提交答案