Skip to main content

File Handling in c

File handling is an important aspect of programming in C. It allows a program to read and write data to and from files. This article will explain the basics of file handling in C, and will cover all the major methods that are commonly used.

What is File Handling in C?

File handling in C is a process by which data is read from or written to files. Files are used to store data permanently, which can be accessed by the program when required. In C, files are typically handled using pointers, which are used to access the data in the file.

Opening a File

The first step in file handling is to open a file. In C, this is done using the fopen() function. The function takes two arguments: the name of the file and the mode in which the file is to be opened. The modes in which a file can be opened are:

  • "r" - read mode
  • "w" - write mode
  • "a" - append mode

For example, to open a file named "example.txt" in read mode, the code would be:

FILE *fp;
fp = fopen("example.txt", "r");

Reading Data from a File

Once a file is opened, data can be read from it using functions like fscanf() or fgets(). The fscanf() function reads formatted data from the file, while the fgets() function reads a line of data from the file. Here is an example of how to use the fscanf() function to read data from a file:

FILE *fp;
int num;
fp = fopen("example.txt", "r");
fscanf(fp, "%d", &num);
fclose(fp);

Here is an example code to read all data from a file in C:

#include <stdio.h>

int main() {
    FILE *fp;
    char ch;

    // Open the file
    fp = fopen("example.txt", "r");

    // Check if file was opened successfully
    if (fp == NULL) {
        printf("Error opening file.");
        return 1;
    }

    // Read and print all data from file
    while ((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
    }

    // Close the file
    fclose(fp);

    return 0;
}

In this code, we first declare a pointer to a file (fp) and a variable to store each character we read from the file (ch). We then use the fopen() function to open the file "example.txt" in read mode ("r").

We then use a while loop to read each character from the file using the fgetc() function. The loop continues until we reach the end of the file (EOF), which is indicated by the function returning a value of -1. Each character is printed to the console using printf().

Writing Data to a File

Data can be written to a file using functions like fprintf() or fputs(). The fprintf() function writes formatted data to the file, while the fputs() function writes a string to the file. Here is an example of how to use the fprintf() function to write data to a file:

FILE *fp;
int num = 10;
fp = fopen("example.txt", "w");
fprintf(fp, "%d", num);
fclose(fp);

Closing a File

After a file has been read from or written to, it should be closed using the fclose() function. This ensures that any changes made to the file are saved, and that the file is available for other programs to access. Here is an example of how to use the fclose() function:

FILE *fp;
fp = fopen("example.txt", "r");
// read data from file
fclose(fp);

Appending Data to a File

If data needs to be added to the end of a file, the file can be opened in append mode using the "a" mode argument when calling fopen(). Here is an example of how to use append mode:

FILE *fp;
fp = fopen("example.txt", "a");
// write data to file
fclose(fp);

Error Handling

When working with files, it is important to handle errors properly. The fopen() function can return a NULL pointer if there is an error in opening the file. This can be checked using an if statement. Here is an example of how to handle errors when opening a file:

FILE *fp;
fp = fopen("example.txt", "r");
if (fp == NULL) {
    printf("Error opening file.");
    exit(1);
}
// read data from file
fclose(fp);

Conclusion

File handling is an important aspect of programming in C. In this article, we covered the basics of file handling, including how to open, read from, write to, append to, and close a file. We also discussed error handling when working





Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment