File Handling in C

Mannan Ul Haq
0
In C, file handling is performed using the standard I/O library, which provides functions to work with files. Files are an essential part of data storage and retrieval in many applications. C provides various functions for creating, opening, reading, writing, and closing files. Here's an overview of working with files in C:

File Pointers:

Before working with a file, you need to declare a file pointer of type `FILE`. The file pointer will be used to interact with the file. Here's how you declare a file pointer:

#include <stdio.h>

FILE* filePointer;

Opening a File:

To open a file, you can use the `fopen()` function. The function takes two arguments: the name of the file and the mode in which you want to open the file (e.g., read, write, append, etc.).

filePointer = fopen("filename.txt", "mode");

The "mode" argument can have the following values:
  • `"r"`: Read mode.
  • `"w"`: Write mode. If the file already exists, its content will be truncated. If the file doesn't exist, a new file is created.
  • `"a"`: Append mode. Data is written at the end of the file. If the file doesn't exist, a new file is created.

Reading from a File:

To read data from a file, you can use functions like `fscanf()` or `fgets()`.
  • `fscanf()` reads formatted data from the file.
  • `fgets()` reads a line of text from the file.

Writing to a File:

To write data to a file, you can use functions like `fprintf()` or `fputs()`.
`fprintf()` writes formatted data to the file.
`fputs()` writes a string to the file.

Closing a File:

Always remember to close the file after you are done working with it. To close a file, use the `fclose()` function:

fclose(filePointer);

Example: Writing to a File

We will write some text to a file named "output.txt".

#include <stdio.h>

int main()
{
    FILE* filePointer;
    char text[] = "This is the content to be written to the file.\n";

    filePointer = fopen("output.txt", "w");
    if (filePointer == NULL)
    {
        printf("Error opening the file.\n");
        return 1;
    }

    fprintf(filePointer, "%s", text);
    fclose(filePointer);

    printf("Content written to the file successfully.\n");
    return 0;
}

After running this program, you will find a file named "output.txt" in the same directory containing the text "This is the content to be written to the file".

Please note that when using `"w"` mode for file writing, if the file already exists, it will be truncated (emptied), and if it doesn't exist, a new file will be created. If you want to append data to an existing file or create a new file if it doesn't exist, you can use `"a"` mode instead.


Example: Reading from a File

Suppose we have a file named "input.txt" with the following content:

Hello, this is a sample file.
We will read this file in C.

#include <stdio.h>

int main()
{
    FILE* filePointer;
    char buffer[100];

    filePointer = fopen("input.txt", "r");
    if (filePointer == NULL)
    {
        printf("Error opening the file.\n");
        return 1;
    }

    while (fgets(buffer, sizeof(buffer), filePointer) != NULL)
    {
        printf("%s", buffer);
    }

    fclose(filePointer);
    return 0;
}

Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !