Files in C

A file is a collection of data stored in the secondary memory. The reasons to use files are because RAM is volatile, contents are lost when the program terminates and files are used to persist the data. The operation of files is to create a new file, open a file, close a file, read from a file and write in a file. There are 2 types of files.

Text files- Stores data in the form of text. eg: .txt, .c

Binary Files- Stores data like .exe, .mp3, .jpg .

File Pointer- A file is a (hidden) structure that needs to be created for opening a file. A FILE ptr that points to this structure and is used to access the file. Opening a file- FILE *fptr; fptr=fopen("filename, mode); and to close a file- fclose(fptr);

File Opening Modes

"r" is open to read (opens the file to just read and not change the text in the files).

"rb" is open to read in binary.

"w" open to write (opens the file to start writing).

"wb" open to write in binary.

"a" opens to append (append is to add words to existing words).

Best Practice- Check if the file exists before reading from it. And to read a file char ch; fscanf(fptr,"%c", &ch); Eg-

 int main()
{
   FILE *fptr;
   fptr=fopen("file.txt","r");

   int ch;
   fscanf(fptr,"%d",&ch);
   printf("Character=%d\n",ch);

   fscanf(fptr,"%d",&ch);
   printf("Character=%d\n",ch);

   fscanf(fptr,"%d",&ch);
   printf("Character=%d\n",ch);


   fclose(fptr);


  return 0;
}(replace %d with %c and int to char to print the character from a different file)

Writing to a file

char ch = 'A';
fprintf(fptr, "%c", ch);

eg: int main()
{
   FILE *fptr;
   fptr=fopen("file.txt","a");

   fprintf(fptr,"%c",'M');
   fprintf(fptr,"%c",'A');
   fprintf(fptr,"%c",'N');
   fprintf(fptr,"%c",'G');
   fprintf(fptr,"%c",'O');

   fclose(fptr);

  return 0;
}

To read and write a char use fetc(fptr) (takes a character for you). And fputc('A',fptr) (adds a character in replacement for you). fgetc returns end of file to show that the file has ended. Eg-

int main()
{
   FILE *fptr;
   fptr=fopen("file.txt","r");
   char ch;
   ch=fgetc(fptr);
   while(ch!=EOF){
      printf("%c",ch);
      ch=fgetc(fptr);
   }
   printf("\n");
   fclose(fptr);
  return 0;
}

Questions to solve

Q1 Make a file to read 5 integers from a file in C.

int main()
{
   FILE*fptr;
   fptr=fopen("file.txt","r");
   int n;
   fscanf(fptr,"%d",&n);
   printf("number=%d\n",n);

   fscanf(fptr,"%d",&n);
   printf("number=%d\n",n);

   fscanf(fptr,"%d",&n);
   printf("number=%d\n",n);

   fscanf(fptr,"%d",&n);
   printf("number=%d\n",n);

   fscanf(fptr,"%d",&n);
   printf("number=%d\n",n);

   fclose(fptr);
  return 0;
}

Q2 Write a program to input student information from a user and enter it into a file in C.

int main()
{
   FILE*fptr;
   fptr=fopen("Student.text","w");
   int age;
   int cgpa;
   char name[100];

   printf("enter name: ");
   scanf("%s",&name);
   printf("Enter age : ");
   scanf("%d",&age);
   printf("Enter cgpa: ");
   scanf("%f",&cgpa);

   fprintf(fptr,"%s\t",name);
   fprintf(fptr,"%d\t",age);
   fprintf(fptr,"%f",cgpa);

   fclose(fptr);
  return 0;
}

Q3 Write a program to write all the odd numbers from 1 to n in a file in C.

int main()
{
   FILE*fptr;
   fptr=fopen("odd.text","w");
   int n;
   printf("enter n : ");
   scanf("%d",&n);

   for(int i=0;i<=n;i++){
    if(i%2!=0){
      fprintf(fptr,"%d\n",i);
    }
   }

   fclose(fptr);
  return 0;
}

Q4 Write a program with 2 numbers a and b are written in a file write a program to replace them with their sum in C.

int main()
{
   FILE*fptr;
   fptr=fopen("sum.text","r");
   int a;
   fscanf(fptr,"%d",&a);
   int b;
   fscanf(fptr,"%d",&b);

  fclose(fptr);

fptr=fopen("sum.text","w");
fprintf(fptr,"%d",a+b);
   fclose(fptr);
  return 0;
}

Hope you enjoyed learning about Files, this helps us to store data for the long term, hope this blog was useful. And don't forget to follow and share this blog with a friend who wants to learn about programming.

Thank you...

0
Subscribe to my newsletter

Read articles from Gagan G Saralaya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gagan G Saralaya
Gagan G Saralaya