Tutorials Home   >   Practical Programming Skills   >   File Handling

File Handling

1. Introduction to File Handling

In C programming, file handling is used to store data permanently in a file on a computer. Normally, when a program ends, all data stored in variables is lost. To avoid this problem, files are used to store data permanently so it can be used later.

Why File Handling is Important

  • Data can be stored permanently

  • Large amounts of data can be saved

  • Data can be reused later

  • Used in real-life applications like:

    • Student records

    • Employee details

    • Banking systems

    • Inventory management

C provides several library functions to work with files.


2. Types of Files in C

There are mainly two types of files in C:

1. Text Files

  • Data is stored in human-readable form

  • Easy to read and edit

  • Examples: .txt, .c

  • Uses characters and strings

Example:

Name: sam
Marks: 85

2. Binary Files

  • Data is stored in binary (0s and 1s)

  • Faster and more secure

  • Not human-readable

  • Uses structures and raw data

Example:

010101010101

3. File Pointer

To work with files in C, we use a file pointer.

Definition

A file pointer is a pointer of type FILE which points to a file.

Syntax

FILE *fp;
  • FILE is a structure defined in stdio.h

  • fp is the file pointer


4. Opening a File

To open a file, we use the fopen() function.

Syntax

fp = fopen("filename", "mode");

Example

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

File Opening Modes

Mode Meaning
"r" Read only
"w" Write only
"a" Append
"r+" Read and write
"w+" Write and read
"a+" Append and read

âš  If the file does not exist:

  • "r" → error

  • "w" → creates new file

  • "a" → creates new file


5. Closing a File

After working with a file, it must be closed using fclose().

Syntax

fclose(fp);

Importance of Closing a File

  • Saves data properly

  • Frees memory

  • Prevents data corruption


6. Writing Data to a File

1. Using fprintf()

Used to write formatted data to a file.

Syntax

fprintf(fp, "format", variables);

Example Program

#include <stdio.h>

int main() {
FILE *fp;
fp = fopen(“student.txt”, “w”);

fprintf(fp, “Name: Aman\n”);
fprintf(fp, “Marks: 90\n”);

fclose(fp);
return 0;
}


2. Using fputc()

Used to write one character at a time.

Syntax

fputc(character, fp);

3. Using fputs()

Used to write a string to a file.

Syntax

fputs("Hello", fp);

7. Reading Data from a File

1. Using fscanf()

Reads formatted data from a file.

Syntax

fscanf(fp, "format", &variables);

Example

#include <stdio.h>

int main() {
FILE *fp;
char name[20];
int marks;

fp = fopen(“student.txt”, “r”);
fscanf(fp, “Name: %s\n”, name);
fscanf(fp, “Marks: %d”, &marks);

printf(“Name: %s\nMarks: %d”, name, marks);

fclose(fp);
return 0;
}


2. Using fgetc()

Reads one character at a time.

Syntax

ch = fgetc(fp);

3. Using fgets()

Reads a line of text.

Syntax

fgets(string, size, fp);

8. End of File (EOF)

When reading a file, we need to know when the file ends. C uses EOF (End Of File).

Example

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

9. Binary File Handling

Binary files are used to store data in binary format.

Functions Used

  • fwrite()

  • fread()


Writing to a Binary File

Syntax

fwrite(&variable, sizeof(variable), 1, fp);

Example

#include <stdio.h>

struct student {
int roll;
float marks;
};

int main() {
FILE *fp;
struct student s = {1, 85.5};

fp = fopen(“data.bin”, “wb”);
fwrite(&s, sizeof(s), 1, fp);

fclose(fp);
return 0;
}


Reading from a Binary File

Syntax

fread(&variable, sizeof(variable), 1, fp);

10. File Handling Functions Summary

Function Purpose
fopen() Open a file
fclose() Close a file
fprintf() Write formatted data
fscanf() Read formatted data
fputc() Write a character
fgetc() Read a character
fputs() Write a string
fgets() Read a string
fwrite() Write binary data
fread() Read binary data

11. Advantages of File Handling

  • Permanent data storage

  • Easy data retrieval

  • Useful for large data

  • Supports real-world applications


12. Conclusion

File handling in C is an important concept that allows programs to store and retrieve data permanently. By using file pointers and file functions, we can easily read and write data to files. Understanding file handling helps in developing practical and real-world applications.