gpt4 book ai didi

c - 如何在附加后打印文件中的值

转载 作者:行者123 更新时间:2023-11-30 17:17:35 24 4
gpt4 key购买 nike

我有以下问题:我必须制作一份包含姓名、年龄和成绩的学生文件。它工作正常,但问题是在屏幕上打印所有内容时。假设我读了 3 个学生,当我输入“L”在屏幕上打印内容时效果很好。当我按“A”将另一个学生添加到文件中(追加)时,该学生将出现在我的文件中(因此我们总共有 4 个学生),但是当我再次按“L”打印时,它只打印 3共 4 名学生,包括附加的一名学生,但删除了另一名学生。 'S' 代表写入文件。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

struct student{
char nume[20];
int varsta;
float medie;
};

void citire_date(struct student *studenti, FILE *f, int n){
int i;

for(i=0;i<n;i++){
printf("Introduceti datele studentului %d\n", i+1);
printf("Nume: "); fflush(stdin); gets((studenti+i)->nume);
printf("Varsta: "); scanf("%d", &((studenti+i)->varsta));
printf("Medie: "); scanf("%f", &((studenti+i)->medie));
fprintf(f, "Nume: %s\n", (studenti+i)->nume);
fprintf(f, "Varsta: %d\n", (studenti+i)->varsta);
fprintf(f, "Medie: %.2f\n", (studenti+i)->medie);
fprintf(f, "\n");
}
}

void afisare_date(struct student *studenti, int n){
int i;
char nume1;
int varsta1;
float medie1;

for(i=0;i<n;i++){
printf("Nume: %s\n", (studenti+i)->nume);
printf("Varsta: %d\n", (studenti+i)->varsta);
printf("Medie: %.2f\n", (studenti+i)->medie);
printf("\n");
}
}

void append(struct student *studenti, FILE *f){
int i;

for(i=0;i<1;i++){
printf("Nume: "); fflush(stdin); gets((studenti+i)->nume);
printf("Varsta: "); scanf("%d", &((studenti+i)->varsta));
printf("Medie: "); scanf("%f", &((studenti+i)->medie));
fprintf(f, "Nume: %s\n", (studenti+i)->nume);
fprintf(f, "Varsta: %d\n", (studenti+i)->varsta);
fprintf(f, "Medie: %.2f\n", (studenti+i)->medie);
fprintf(f, "\n");
}
}

int main()
{
FILE *f;
struct student studenti[20];
int n;
char optiune;

f=fopen("studenti.txt", "w");
if(f==NULL){
printf("Nu s-a putut deschide/crea fisierul pentru scriere.\n");
exit(1);
}

printf("\n");

do{
printf("S. Scriere fisier.\n");
printf("A. Adaugarea unui nou student in fisier.\n");
printf("L. Listarea datelor tuturor studentilor.\n");
printf("M. Modificarea datelor unui student.\n");
printf("C. Cautarea unui student dupa nume.\n");
printf("X. Terminarea programului.\n");
printf("Introduceti optiunea: "); fflush(stdin); optiune=toupper(getchar());
printf("\n");


switch(optiune){
case 'S':
printf("Introduceti numarul de studenti: "); scanf("%d", &n);
citire_date(studenti, f, n);
fclose(f);
printf("\n");
break;
case 'A':
f = fopen("studenti.txt", "a");
if(f==NULL ){
printf("Nu s-a putut deschide fisierul pentru adaugare.\n");
exit(2);
}
append(studenti, f);
fclose(f);
break;
case 'L':
afisare_date(studenti, n);
break;
case 'M':
break;
case 'C':
break;
case 'X':
break;
default:
printf("\nAti introdus o optiune eronata!\n");
break;

}
}while(optiune!='X');





return 0;
}

我想我需要使用fscanf,但我不知道它会如何从文件中读取“Nume:name”“Varsta:19”等。预先感谢您。

最佳答案

我编辑了我的主目录,所以现在一切都应该清晰可见。

int main()
{
FILE *f;
struct student studenti[20];
int n;
char optiune;

printf("Introduceti numarul de studenti: "); scanf("%d", &n);

f=fopen("studenti.txt", "w");
if(f==NULL){
printf("Nu s-a putut deschide/crea fisierul pentru scriere.\n");
exit(1);
}
citire_date(studenti, f, n);
fclose(f);

printf("\n");

do{
printf("A. Adaugarea unui nou student in fisier.\n");
printf("L. Listarea datelor tuturor studentilor.\n");
printf("M. Modificarea datelor unui student.\n");
printf("C. Cautarea unui student dupa nume.\n");
printf("X. Terminarea programului.\n");
printf("Introduceti optiunea: "); fflush(stdin); optiune=toupper(getchar());
printf("\n");


switch(optiune){
case 'A':
f = fopen("studenti.txt", "a");
if(f==NULL ){
printf("Nu s-a putut deschide fisierul pentru adaugare.\n");
exit(2);
}
append(studenti, f);
fclose(f);
break;
case 'L':
afisare_date(studenti, n);
break;
case 'M':
break;
case 'C':
break;
case 'X':
break;
default:
printf("\nAti introdus o optiune eronata!\n");
break;

}
}while(optiune!='X');





return 0;
}

现在有什么建议吗?

关于c - 如何在附加后打印文件中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29398073/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com