File handling in C enables us to create, update, read, and delete the files stored on the local file system through our C program. The following operations can be performed on a file.

File handling is an essential aspect of C programming, allowing you to interact with files on your system. This includes reading data from files, writing data to files, and performing various operations on files.

Basic File Operations

  1.  Opening a File:

    • Use the fopen function to open a file.
    • Syntax: FILE *fp = fopen(filename, mode);
      • filename: The name of the file to be opened.
      • mode: The mode in which the file is opened. Common modes include:
        • "r": Read mode (open an existing file for reading).
        • "w": Write mode (create a new file or overwrite an existing file).
        • "a": Append mode (append data to the end of an existing file).
    • If the file is opened successfully, fp will point to the file. Otherwise, it will be NULL.
  2.  Reading from a File:

    • Use the fscanf function to read formatted data from a file.
    • Syntax: fscanf(fp, format_string, &var1, &var2, ...);
      • fp: The file pointer.
      • format_string: A format string specifying the type of data to be read.
      • &var1, &var2, …: Pointers to variables where the read data will be stored.
  3.  Writing to a File:

    • Use the fprintf function to write formatted data to a file.
    • Syntax: fprintf(fp, format_string, var1, var2, ...);
      • fp: The file pointer.
      • format_string: A format string specifying the type of data to be written.
      • var1, var2, …: The values to be written to the file.
  4.  Closing a File:

    • Use the fclose function to close a file.
    • Syntax: fclose(fp);

Example:

#include <stdio.h>

int main() {
    FILE *fp;
    char str[100];

    // Open a file for reading
    fp = fopen("data.txt", "r");
    if (fp == NULL) {
        printf("Error opening filen");
        return 1;
    }

    // Read a line from the file
    fgets(str, 100, fp);
    printf("Read: %sn", str);

    // Close the file
    fclose(fp);

    return 0;
}

 

Functions for File Handling

No. Function Description
1 fopen() Opens a new or existing file
2 fprintf() Writes data into the file
3 fscanf() Reads data from the file
4 fputc() Writes a character into the file
5 fgetc() Reads a character from the file
6 fclose() Closes the file
7 fseek() Sets the file pointer to a given position
8 fputw() Writes an integer to the file
9 fgetw() Reads an integer from the file
10 ftell() Returns the current position
11 rewind() Sets the file pointer to the beginning of the file

 

Additional Considerations:

  • Error Handling: Always check the return values of file operations to ensure they are successful.
  • Binary Files: For reading and writing binary data, use functions like fread and fwrite.
  • File Pointers: You can use fseek to move the file pointer to a specific position within the file.
  • File Permissions: Ensure that your program has the necessary permissions to read and write files.

 

Related Posts

Leave a Reply