In this article, we will discuss file handling functions in C, along with examples and programs to understand how each function works. File handling allows us to store, read, and manipulate data in files, making it permanent and reusable.

File Handling Functions in C

  1. fputc() Function

The fputc() function is used to write a single character to a file.

Syntax:

fputc(character, file_pointer);

  • character: The character to write in the file.
  • file_pointer: The pointer to the file.

Example:

#include <stdio.h>

int main() {

char ch;

FILE *ptr;

ptr = fopen(“c:\\demo.txt”, “w”);

if(ptr == NULL) {

printf(“File could not be created or opened.\n”);

} else {

printf(“Enter a single character: “);

scanf(“%c”, &ch);

fputc(ch, ptr);

fclose(ptr);

printf(“File created and character written successfully.\n”);

}

return 0;

}

Output:

Enter a single character: a

File created successfully.

  1. fputs() Function

The fputs() function writes a string to a file.

Syntax:

fputs(string, file_pointer);

Example:

#include <stdio.h>

int main() {

char name[20];

FILE *ptr;

ptr = fopen(“c:\\demo2.txt”, “w”);

if(ptr == NULL) {

printf(“File could not be created or opened.\n”);

} else {

printf(“Enter your name: “);

scanf(“%s”, name);

fputs(name, ptr);

fclose(ptr);

printf(“File created and string written successfully.\n”);

}

return 0;

}

Output:

Enter your name: Yashraj

File created successfully.

  1. fgetc() Function

The fgetc() function is used to read a single character from a file. It is often used in a loop until the end of file (EOF).

Syntax:

fgetc(file_pointer);

Example:

#include <stdio.h>

int main() {

char ch;

FILE *ptr;

ptr = fopen(“c:\\demo2.txt”, “r”);

if(ptr == NULL) {

printf(“File not opened.\n”);

} else {

printf(“Content of the file:\n”);

while((ch = fgetc(ptr)) != EOF) {

printf(“%c”, ch);

}

fclose(ptr);

}

return 0;

}

Output:

Content of the file:

Yashraj

  1. fprintf() Function

The fprintf() function writes formatted data to a file, similar to printf().

Syntax:

fprintf(file_pointer, “format”, variables);

Example:

#include <stdio.h>

int main() {

char name[20];

int roll;

float cgpa;

FILE *ptr;

ptr = fopen(“c:\\demo3.txt”, “w”);

if(ptr == NULL) {

printf(“File could not be opened.\n”);

} else {

printf(“Enter roll number: “);

scanf(“%d”, &roll);

printf(“Enter name: “);

scanf(“%s”, name);

printf(“Enter CGPA: “);

scanf(“%f”, &cgpa);

fprintf(ptr, “Roll: %d, Name: %s, CGPA: %.2f”, roll, name, cgpa);

fclose(ptr);

printf(“Data written to file successfully.\n”);

}

return 0;

}

Output:

Enter roll number: 10

Enter name: Rajveer

Enter CGPA: 9.7

Data written to file successfully.

  1. fscanf() Function

The fscanf() function reads formatted data from a file.

Syntax:

fscanf(file_pointer, “format”, &variable);

Example:

#include <stdio.h>

int main() {

char name[20];

FILE *ptr;

char buff[255];

ptr = fopen(“c:\\demo3.txt”, “r”);

if(ptr == NULL) {

printf(“File not opened.\n”);

} else {

printf(“Data in file:\n”);

while(fscanf(ptr, “%s”, buff) != EOF) {

printf(“%s “, buff);

}

fclose(ptr);

}

return 0;

}

Output:

Data in file:

Roll: 10, Name: Rajveer, CGPA: 9.70

  1. fwrite() and fread() Functions

  • fwrite() writes data block-wise to a file.
  • fread() reads data block-wise from a file.

Example:

#include <stdio.h>

int main() {

FILE *ptr;

char str[20] = “Have a nice day!”;

char str2[20];

ptr = fopen(“c:\\test.txt”, “w”);

fwrite(str, sizeof(str)-1, 1, ptr);

fclose(ptr);

ptr = fopen(“c:\\test.txt”, “r”);

fread(str2, sizeof(str), 1, ptr);

printf(“%s\n”, str2);

fclose(ptr);

return 0;

}

Output:

Have a nice day!

  1. rewind() Function

Resets the file pointer to the beginning of the file.

Syntax:

rewind(file_pointer);

  1. ftell() Function

Returns the current position of the file pointer.

Syntax:

long ftell(FILE *stream);

Example:

#include <stdio.h>

#include <string.h>

int main() {

FILE *ptr;

char str[20] = “Hello World”;

int length;

ptr = fopen(“c:\\test2.txt”, “w”);

fputs(str, ptr);

fclose(ptr);

ptr = fopen(“c:\\test2.txt”, “r”);

fgets(str, sizeof(str), ptr);

length = ftell(ptr);

printf(“File pointer position: %d\n”, length);

rewind(ptr);

length = ftell(ptr);

printf(“File pointer after rewind: %d\n”, length);

fclose(ptr);

return 0;

}

Output:

File pointer position: 11

File pointer after rewind: 0

  1. fseek() Function

Sets the file pointer to a specific position in the file.

Syntax:

int fseek(FILE *stream, long offset, int origin);

  1. fflush() Function

Clears the input/output buffer associated with a file.

Syntax:

fflush(file_pointer);

Advantages of File Handling Functions in C

  1. Store program data permanently.
  2. Handle large amounts of data easily.
  3. Random access to any part of the file using fseek() and ftell().
  4. Transfer files between computers or programs conveniently.

Some More: 

POP- Introduction to Programming Using ‘C’

DS – Data structure Using C

OOP – Object Oriented Programming 

Java Programming

DBMS – Database Management System

RDBMS – Relational Database Management System

Join Now: Data Warehousing and Data Mining 

Leave a Reply

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