Pages

Thursday, October 25, 2018

Updating File Data using fseek


File Management - Updating File Records


Following program will allow us to change marks of roll no. 101 using fseek() function. Content of "student.txt" (RollNo, Marks, Name) file is as under:

101 50 AAA
102 60 BBB
103 65 CCC

#include <stdio.h>

int main () {
   FILE *fp;
   char str[80];
   int newmarks;
   
   fp = fopen("student.txt","r+");

   printf("Original File Data:\n");
   while(fgets(str,80,fp)!=NULL)
    printf("%s", str);
   //getch();
   
   printf("\n\nEnter new marks of Roll no. 101:");
   scanf("%d", &newmarks);
      
   printf("Updated File Data:\n\n");
   
   itoa(newmarks,str, 10); //convert int to string; fputs() write "string" to fp
   
   fseek(fp, 4, 0); //set the pointer to position no. 4
   
   fputs(str, fp);  //write string to current position of fp
   fclose(fp);
   
   fp = fopen("student.txt","r");

   while(fgets(str,80,fp)!=NULL)
    printf("%s", str);
   
   fclose(fp);
   getch();
   return(0);
}


Output of Program

Original File Data:
101 55 AAA
102 60 BBB
103 65 CCC

Enter new marks of Roll no. 101:75

Updated File Data:
101 75 AAA
102 60 BBB
103 65 CCC

No comments:

Post a Comment