×
>
<

Aptitude

File Handling in C

Introduction to File Handling in C Programming.

 

File handling is a crucial aspect of programming that allows you to read from and write to files, enabling your programs to interact with external data sources or persist information for future use. In the C programming language, file handling is facilitated through a set of functions provided by the standard input/output library (stdio.h).

File Handling Formats in C

In C programming, file handling allows you to work with files stored on the system. Here are some of the file handling formats available in C:

  • Text Files: These files store data in a human-readable format. They contain characters and can be opened using text editors.
  • Binary Files: Binary files store data in a format that is not human-readable. They contain a sequence of bytes and are used for storing data in a more compact and efficient manner.
  • Sequential Access Files: In sequential access files, data is read or written sequentially from the beginning to the end of the file. This means you can only read or write data in the order it appears in the file.
  • Random Access Files: Random access files allow you to read or write data at any position within the file. This provides more flexibility in accessing and manipulating data.
Modes of File Handling in C
Mode Description
Read Mode (r) Opens a file for reading. The file must exist, otherwise fopen() will return NULL.
Write Mode (w) Opens a file for writing. If the file does not exist, it creates a new file. If the file already exists, its contents are erased.
Append Mode (a) Opens a file for writing at the end of the file. If the file does not exist, it creates a new file.
Read/Write Mode (r+) Opens a file for both reading and writing. The file must exist.
Write/Read Mode (w+) Opens a file for reading and writing. If the file exists, its contents are erased. If the file does not exist, it creates a new file.
Append/Read Mode (a+) Opens a file for reading and appending. Data can be read from and written to the file. If the file does not exist, it creates a new file.
Read Mode (r) Example

Reads data from a file.


#include 
#include 

int main() {
    FILE *filePointer;
    char data[1000];

    filePointer = fopen("example.txt", "r");

    if (filePointer == NULL) {
        printf("File does not exist or cannot be opened.\n");
        exit(1);
    }

    fgets(data, 1000, filePointer);
    printf("Data read: %s\n", data);

    fclose(filePointer);
    return 0;
}

Write Mode (w) Example

Writes data to a file, erasing its previous contents if it exists, and creates a new file if it does not exist.


#include 

int main() {
    FILE *filePointer;

    filePointer = fopen("example.txt", "w");

    if (filePointer == NULL) {
        printf("File cannot be opened.\n");
        return -1;
    }

    fprintf(filePointer, "This is an example text written to the file.");

    fclose(filePointer);
    return 0;
}

Append Mode (a) Example

Appends data to the end of a file, creating a new file if it does not exist.


#include 

int main() {
    FILE *filePointer;

    filePointer = fopen("example.txt", "a");

    if (filePointer == NULL) {
        printf("File cannot be opened.\n");
        return -1;
    }

    fprintf(filePointer, "This is an example text appended to the file.");

    fclose(filePointer);
    return 0;
}

Read/Write Mode (r+) Example

Opens a file for both reading and writing, without deleting its content.


#include 

int main() {
    FILE *filePointer;

    filePointer = fopen("example.txt", "r+");

    if (filePointer == NULL) {
        printf("File cannot be opened.\n");
        return -1;
    }

    fprintf(filePointer, "This is an example text written to the file.");

    fclose(filePointer);
    return 0;
}

Write/Read Mode (w+) Example

Opens a file for reading and writing, erasing its previous contents if it exists, and creates a new file if it does not exist.


#include 

int main() {
    FILE *filePointer;

    filePointer = fopen("example.txt", "w+");

    if (filePointer == NULL) {
        printf("File cannot be opened.\n");
        return -1;
    }

    fprintf(filePointer, "This is an example text written to the file.");

    fclose(filePointer);
    return 0;
}

Append/Read Mode (a+) Example

Opens a file for reading and appending data to it, creating a new file if it does not exist.


#include 

int main() {
    FILE *filePointer;

    filePointer = fopen("example.txt", "a+");

    if (filePointer == NULL) {
        printf("File cannot be opened.\n");
        return -1;
    }

    fprintf(filePointer, "This is an example text appended to the file.");

    fclose(filePointer);
    return 0;
}