我正在尝试用 c 语言制作一个学生详细信息记录程序,其中我将所有数据存储在一个文件中。 我将为用户提供诸如输入新记录、显示等选项。 如何使用户输入复制到文件的下一行?
示例:
当前文件 -->
阿克沙特 15 96
罗伊57 67
用户输入-->
约翰作为名字
33 为 USN
87 分
最终文件输出
阿克沙特 15 96
罗伊57 67
约翰福音 33 87
最佳答案
尝试使用“a”模式打开文件,例如:file=fopen("FileName.dat","a");
然后通过读取用户的输入来正常填充它,然后将它们写入您的文件中:
示例:
#include <stdio.h>
typedef struct {
char name[60];
int age;
}E;
E temp;
/*lets say that the file is already filled and you want to append more data to it*/
int main (void) {
FILE *file;
file=fopen("FileName.dat","a");
scanf("%s",temp.name);
scanf("%d",&temp.age);
//after you read the inputs from the user write the data into your file
fwrite(&temp,sizeof(E),1,file);
//dont forget to close the file
fclose(file);
return 0;
}
关于c - 将文件指针移至末尾,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48440074/