gpt4 book ai didi

c - 将每个变量打印到文件中

转载 作者:行者123 更新时间:2023-11-30 15:22:48 25 4
gpt4 key购买 nike

下午好。我正在打印到文件,但遇到了一些问题。首先,程序从文件中读取内容,将内容存储在变量中,然后使用一些附加信息打印到另一个文件。然而问题是,它只将第一个文件中最后一个人的信息打印到新文件中。如果有人能够解决这个问题,我将非常感激。下面是代码片段

#include <stdio.h>
#include <conio.h>
#include <errno.h>

int idArray[100];
int AmtArray; //stores the number of ID's in array

int Stud_List();
//FUNCTION TO PRINT STUDENT'S LIST
int Stud_List(){
//DECLARATION OF VARIABLES
FILE *kfile;
char ffname[28];
char flname[28];
int fID;
int x; //counts amount of entries within file

//INITIALIZATION OF VARIABLES
x = 0;

if ((kfile = fopen("information.txt", "r")) == NULL)
{
//this to be displayed if file path is incorrect or file cannot be found.
fprintf(stderr, "Error while opening file (%d: %s)\n", errno, strerror(errno));
return 0;
}

while (!feof(kfile)){

fscanf(kfile, "%s %s %d", ffname, flname, &fID); //reads the contents of the file
printf("\n\n%s - %s - %d", ffname ,flname, fID);
idArray[x] = fID;
x++;
}
AmtArray = x;
}

int Stats( char *, int);
int Stats (*status, int id){

FILE *Register;

Register = fopen ("update.txt","w");
fprintf( Register, "%5d%8s", id, status);

fclose(Register);
}

int main (void){
int IDentry;
int x, y; //counters in ID check
printf(" ========Attendance Recording System========\n");
printf(" Menu\n");

Stud_List(); //call to the function to print the list of the students

printf("\n\n");
for (x = 0; x < AmtArray; x++){

printf("Please enter ID for absent student: ");
scanf("%4d[0123456789]", &IDentry);

if (IDentry == idArray[x]){
Stats ("Absent", idArray[x]);
}
if (IDentry == idArray[x]){
Stats ("Absent", idArray[x]);
}
}
getch();
return 0;
}

最佳答案

Register = fopen ("update.txt","w");

每次打开文件时都会清除该文件。第二个参数应更改为 "a",以便每次调用 Stats() 都会附加到文件末尾。

关于c - 将每个变量打印到文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29038905/

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