bscodinglab

File Handling

So far the operations using the C program are done on a prompt/terminal which is not stored anywhere. But in the software industry, most programs are written to store the information fetched from the program. One such way is to store the fetched information in a file.

 Different operations that can be performed on a file are  :-

  1. Creation of a new file (fopen() with attributes as “a” or “a+” or “w” or “w+”)
  2. Opening an existing file (fopen())
  3. Reading from file (fscanf() or fgets())
  4. Writing to a file (fprintf() or fputs())
  5. Moving to a specific location in a file (fseek(), rewind())
  6. Closing a file (fclose())

In order to understand why file handling makes programming easier, let us look at a few reasons :-

  1. Reusability :- The file-handling process keeps track of the information created after the program has been run.
  2. Portability :-Without losing any data files can be transferred to another in the computer system. The risk of flawed coding is minimized with this feature.
  3. Efficient :- A large amount of input may be required for some programs. File handling allows you to easily access a part of a code using individual commands which saves a lot of time and reduces the chance of errors.
  4. Storage Capacity :- Files allow you to store data without having to worry about storing everything simultaneously in a program.

Examlpe : –

                  #include <stdio.h>

               int main()

 {

                          FILE *filePointer;

                       char data[100];

                     // Opening a file in write mode

                    filePointer = fopen(“example.txt”, “w”);

                   if (filePointer == NULL)

                    {

                           printf(“Unable to open the file.\n”);

                            return 1;

                    }

                         printf(“Enter data to write into the file: “);

                         fgets(data, sizeof(data), stdin);

                     // Writing data to the file

                     fprintf(filePointer, “%s”, data);

                    // Closing the file

                     fclose(filePointer);

                    printf(“Data written to the file successfully.\n”);

                    // Opening the file in read mode

                    filePointer = fopen(“example.txt”, “r”);

                    if (filePointer == NULL)

                          {

                                          printf(“Unable to open the file.\n”);

                                          return 1;

                                }

                     printf(“Reading data from the file:\n”);

                     // Reading data from the file

                    while (fgets(data, sizeof(data), filePointer) != NULL)

                            {

                                          printf(“%s”, data);

                             }

                      // Closing the file

                       fclose(filePointer);

                        return 0;

                  }