C Programming

⌘K
  1. Home
  2. Docs
  3. C Programming
  4. File Handling in C
  5. Concept of File in C

Concept of File in C

Learn the concept of file in C programming — including definition, file types, file handling functions, and real examples using fopen(), fclose(), fprintf(), fscanf(), and more.


Introduction: What is a File in C Programming?

A file in C programming is a collection of data stored permanently on a storage device such as a hard disk.

  • Unlike variables that store temporary data in RAM, files allow programs to store, retrieve, and manipulate data permanently — even after the program ends.

File handling in C enables you to perform operations like creating, reading, writing, appending, and closing files efficiently.


Need for File Handling in C

Without file handling, all program data would be lost once execution ends.
File handling helps:

  • Store large amounts of data permanently.
  • Retrieve and modify data anytime.
  • Manage user information, logs, or configurations.
  • Create backup and report systems.

Types of Files in C

C language supports two types of files:

  1. Text Files (.txt)
    • Data stored in human-readable form (ASCII).Example: names, numbers, or words separated by spaces or new lines.Easy to open and edit with any text editor.
    Example:
Hello
123
Welcome to C Programming
  1. Binary Files (.bin)
    • Data stored in binary format (0s and 1s).
    • Faster to read/write and more memory-efficient.
    • Not human-readable but suitable for complex data storage like images or records.

File Operations in C

OperationDescriptionFunction Used
Create/Open a fileOpens or creates a file for I/O operationsfopen()
Read dataReads data from a filefscanf(), fread()
Write dataWrites data to a filefprintf(), fwrite()
Close fileCloses the opened filefclose()
Move pointerMoves file position indicatorfseek(), rewind()

Basic File Handling Example in C

#include <stdio.h>

int main() {
    FILE *fp;  // File pointer
    fp = fopen("example.txt", "w");  // Open file in write mode

    if (fp == NULL) {
        printf("Error opening file!");
        return 1;
    }

    fprintf(fp, "Hello, this is a file handling example in C.\n");
    fclose(fp);

    printf("Data written successfully!\n");
    return 0;
}

Explanation:

  • FILE *fp declares a file pointer.
  • fopen() opens a file in a specific mode ("w" for write).
  • fprintf() writes formatted data to the file.
  • fclose() closes the file and releases resources.

Common File Handling Functions

FunctionDescriptionExample
fopen()Opens a file in different modesfopen("data.txt", "r");
fclose()Closes an opened filefclose(fp);
fprintf()Writes formatted data to a filefprintf(fp, "Name: %s", name);
fscanf()Reads formatted data from a filefscanf(fp, "%d", &num);
fgets()Reads string from filefgets(str, 100, fp);
fputs()Writes string to filefputs("Hello", fp);
fseek()Moves file pointer to specific locationfseek(fp, 0, SEEK_SET);
ftell()Returns current position of file pointerftell(fp);
rewind()Resets file pointer to startrewind(fp);

File Modes in C

ModeDescription
"r"Opens file for reading
"w"Opens file for writing (overwrites existing content)
"a"Opens file for appending
"r+"Opens file for reading and writing
"w+"Opens file for reading and writing (overwrites)
"a+"Opens file for reading and appending

Why File Handling Matters

  • Ensures data persistence after program termination.
  • Facilitates data sharing between programs.
  • Simplifies storage management for large datasets.
  • Enables building real-world applications such as billing systems, databases, and logs.

Conclusion

The concept of file in C forms the foundation for data management and persistence in programming.
By mastering file operations like fopen(), fprintf(), fscanf(), and fclose(), developers can create efficient, real-world C applications that handle data smoothly.

Tags , , , ,

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *