C File I/O

Learn file processing methods in C.

* A file is a collection of data or information that is stored on a computer or digital storage device. Files can contain a wide range of data types, including text, images, videos, audio, programs, and more.

File Processing

File processing in the C programming language involves reading from and writing to files. This is commonly done for tasks like reading data from a file, writing data to a file, or modifying an existing file. The file processing involves the following operations:

  1. Creation of a file of a specific type.
  2. Reading/Processing a file.
  3. Append/Add information to a file.
  4. Modify/Edit data in a file.
  5. Delete items in a file.
  6. Update the file.

File Declaration:

You can declare a file using the FILE structure provided by the standard C library. The FILE structure is a data structure that represents a file stream, which can be used for various file operations. To declare a file, you typically follow these steps:

  • Declare a pointer to a 'FILE' structure:

To work with files, you declare a pointer to a 'FILE' structure, which you'll use to perform file operations. For example:

FILE *fp;

Note: That the type name 'FILE' is always written in capital letters (upper case) and is defined with a typedef declaration. The file pointer develops a link between the storage device and the program.

  • Open a file for reading or writing:

To work with a file, you typically need to open it first. You can use functions like fopen to open a file. Here's an example of opening a file for reading:

fp = fopen("filename.txt", "r");

* The fopen() function is used to open a file and set the file pointer to the beginning/end of file (EOF).

In the code above, "filename.txt" is the name of the file you want to open, and "r" specifies that you want to open the file in read mode. You can use different modes like "w" for write, "a" for append, and more, depending on your needs.

  • Check if the file was opened successfully:

It's essential to check whether the file was opened successfully before performing any file operations. The 'fopen' function returns a 'NULL' pointer if the file couldn't be opened. You can check this by comparing the file pointer to 'NULL':

if (fp == NULL) {
    // Handle the error
    perror("Unable to open the file");
    // You can add error-handling code here
} else {
    // File opened successfully; you can now perform file operations
}
  • Perform file operations:

Once the file is open, you can use various functions, such as 'fread', 'fwrite', 'fprintf', 'fscanf', etc., to read from or write to the file.

  • Close the file when you're done:

It's essential to close the file using the 'fclose' function when you've finished working with it to free up system resources and ensure that any pending writes are flushed to the file.

fclose(fp);

Here's a complete example of opening and reading a text file in C:

example.txt
txt (Text File) Copy Code
The file opened & read successfully.
main.c
c (Source File) Copy Code
#include <stdio.h>

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

    fp = fopen("example.txt", "r");
    
    if (fp == NULL) {
        perror("Unable to open the file");
        return 1;
    }

    while ((ch = fgetc(fp)) != EOF) {
        printf("%c", ch);
    }

    fclose(fp);

    return 0;
}
Output:
The file opened & read successfully.

In this example, we open a file named "example.txt" in read mode, read and print its content character by character, and finally close the file.

In the next chapter, we'll learn how to handle a file more efficiently in C language.

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.