0
1.4kviews
What are various types of files? What are the various modes in which a file can be opened? Explain by giving examples.

What are various types of files? What are the various modes in which a file can be opened? Explain by giving examples.

1 Answer
1
18views

The Random Access Memory is volatile and its content is lost once the program terminates. In order to persist the data format forever we use files.

A file is a data data stored in a storage device. A C program can talk to the file by reading content from it and writing content to it.


Types of Files :-

  1. Text files (.ext, c)
  2. Binary files (.jpg, .dat)


Let's discuss it -

  1. Text Files - Text files are regular files that contain information readable by the user. This information is stored in ASCII. You can display and print these files. The lines of a text file must not contain NUL characters, and none can exceed {LINE_MAX} bytes in length, including the new-line character.


  1. Binary files - Binary files are regular files that contain information readable by the computer. Binary files may be executable files that instruct the system to accomplish a job. Commands and programs are stored in executable, binary files. Special compiling programs translate ASCII text into binary code.

FILE pointer :- A 'FILE' is a structure which needs to be created for opening the file. A FILE pointer is a pointer to the structure of the file. A FILE pointer is needed for communication between the files and the program.

A FILE pointer can be crated as follows :-

FILE *ptr;
ptr = fopen("filename.txt", "mode");

Files opening modes in C :-

Some of the primarily used opening modes in C are -

  1. "r" - Open for reading. If the file does not exist, fopen returns null.
  2. "rb" - Open for reading in binary. If the file does not exist, fopen returns null.
  3. "w" - Open for writing. If the file exists, the content will be overwritten.
  4. "wb" - Open for writing in binary. If the file exists, the content will be overwritten.
  5. "a" - Open for append. If the file does not exist, it will be created.

Reading a file : -

A file can be opened for reading as follows :-

FILE *ptr;
ptr = fopen("abc.txt", "r");
int num;

Let us assume that the "abc.txt" contains an integer. We can read that integer using -

fscanf(ptr, "%d", &num);

fscanf is file counterpart of scanf.

This will read an integer form file in num variable.



Closing the file : -

It is very very important to close the file after read or write.

This is achieved using fclose as follows -

fclose(ptr);

This will tell the compiler that we are done working with this file and the associated resource could be freed.



Writing to a file : -

We can write to a file in a very similar manner like we read the file.

FILE *ptr;
fptr = fopen("abc.txt", "w");

int num = 465;
fprintf(fptr, "%d", num);

fclose(fptr);



fgetc() and fputc() : -

fgetc and fputc are used to write and read a character to / from a file.

fgetc(ptr);    //Used to read a character from a file.

fputc('c', ptr);   //Used to write a character 'c' to a file.



EOF : End of the file : -

fgetc returns EOF when all the characters from a file have been read. So we can write a check like below to detect the end of the file.

while(1){
  ch = fgetc(ptr);
  if(ch == EOF){
  break;
  }
  //------
  //Code
  //------
}

When all the content of the file has been read, it will break the loop !


For better understanding, let's take an example.

Suppose, We have to write a program in C to create and store information in a text file.

The code for the same will be -

#include <stdio.h>
#include <stdlib.h>

int main()
{
   char str[1000];
   FILE *fptr;
   char fname[20]="test.txt";

    printf("\n\n Create a file (test.txt) and input text :\n");
    printf("----------------------------------------------\n"); 
   fptr=fopen(fname,"w");   
   if(fptr==NULL)
   {
      printf(" Error in opening file!");
      exit(1);
   }
   printf(" Input a sentence for the file : ");
   fgets(str, sizeof str, stdin);
   fprintf(fptr,"%s",str);
   fclose(fptr);
   printf("\n The file %s created successfully...!!\n\n",fname);
   return 0;
}
Please log in to add an answer.