将文件指针移动到末尾

Move file pointer to the end

我正在尝试用 c 编写一个学生详细信息记录程序,其中我会将所有数据存储在一个文件中。 我将为用户提供选项,例如输入新记录、显示等。 我怎样才能让用户输入被复制到文件的下一行?

示例:

当前文件 -->

阿克沙特 15 96

罗伊 57 67

用户输入 -->

姓名为约翰

33 作为 USN

87 分

最终文件输出

阿克沙特 15 96

罗伊 57 67

约翰 33 87

您可以使用追加打开文件: file = fopen("myfile.txt", "a"); 然后写一个换行符和你的文本并关闭它。

尝试用“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;
}