'Move file pointer to the end

I am trying to make a students details record program in c wherein I will store all the data in a file. I will provide the user with options such as entering a new record , display , etc. How can i make the user input to be copied at the next line of the file?

Example :

current file -->

Akshat 15 96

Roy 57 67

user input -->

John as name

33 as USN

87 as marks

final file output

Akshat 15 96

Roy 57 67

John 33 87



Solution 1:[1]

You can open the file with append: file = fopen("myfile.txt", "a"); Then write a newline and your text and close it.

Solution 2:[2]

Try to open the file with "a" mode, example: file=fopen("FileName.dat","a"); and then fill it normally by reading the inputs from the user and then write them in your file:

example:

#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;
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Vincent Scharf
Solution 2 Med Rayan