6 Files in C

Files in C Language
In C, a file is a collection of data stored on secondary storage such as a hard disk.
Files are used to store data permanently, unlike variables which store data temporarily in memory.

  1. Need for Files in C

Files are used to:

  • Store data permanently
  • Retrieve data later
  • Handle large amounts of information
  • Save program results
  • Maintain records (students, employees, marks, etc.)
  1. Types of Files in C
  2. Text Files
  • Store data in readable characters
  • Each line ends with a newline character
  • Example: data.txt

Example content:
Ravi 85
Anita 92
John 78

  1. Binary Files
  • Store data in binary form
  • Not human readable
  • Faster for reading and writing

Example: data.dat

  1. File Pointer

In C, a file is handled using a file pointer.
FILE *fp;
Here:

  • FILE is a structure defined in the stdio library
  • fp points to the file being accessed
  1. Opening a File

Files are opened using fopen().
Syntax
fp = fopen(“filename”, “mode”);

Example
FILE *fp;
fp = fopen(“data.txt”, “r”);

  1. File Opening Modes
Mode Meaning
“r” Open file for reading
“w” Open file for writing
“a” Open file for appending
“r+” Read and write
“w+” Read and write (overwrite file)
“a+” Read and append

Example
fp = fopen(“student.txt”,”w”);

  1. Writing to a File

Functions used for writing:

  • fprintf()
  • fputc()
  • fputs()

Example
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen(“data.txt”,”w”);
fprintf(fp,”Hello World”);
fclose(fp);
return 0;

  1. Reading from a File

Functions used for reading:

  • fscanf()
  • fgetc()
  • fgets()

Example
#include <stdio.h>
int main()
{
FILE *fp;
char ch;
fp = fopen(“data.txt”,”r”);
while((ch=fgetc(fp))!=EOF)
{
printf(“%c”,ch);
}
fclose(fp);
return 0;
}

  1. Closing a File

After completing operations, the file must be closed using fclose().
fclose(fp);
This releases the memory used by the file.

  1. Important File Functions
Function Purpose
fopen() Opens a file
fclose() Closes a file
fprintf() Writes formatted data
fscanf() Reads formatted data
fgetc() Reads a character
fputc() Writes a character
fgets() Reads a string
fputs() Writes a string
  1. Basic File Handling Steps
  1. Declare file pointer
  2. Open the file
  3. Perform read/write operations
  4. Close the file

Flow:
Declare pointer

Open file (fopen)

Read / Write data

Close file (fclose)

Definition (Exam Style):
A file in C is a collection of related data stored on secondary storage that allows programs to read and write data permanently.

Text Mode and Binary Mode

Feature Text Mode Binary Mode
Data Storage Data is stored as characters (ASCII text) Data is stored in binary format (0s and 1s)
Readability The file is human readable The file is not human readable
Data Conversion Automatic conversion of special characters like newline (\n) may occur No conversion; data is stored exactly as in memory
File Size Usually larger because numbers are stored as characters Usually smaller since data is stored in compact binary form
Functions Used fprintf(), fscanf(), fgets(), fputs() fread(), fwrite()
File Opening Modes “r”, “w”, “a” “rb”, “wb”, “ab”

Example
Text Mode
FILE *fp;
fp = fopen(“data.txt”,”w”);
fprintf(fp,”Hello”);
Binary Mode
FILE *fp;
fp = fopen(“data.dat”,”wb”);
fwrite(&data, sizeof(data), 1, fp);
Summary

  • Text mode is used for storing readable character data.
  • Binary mode is used for storing raw data exactly as it exists in memory, making it

What is the role of fseek() in C? Solve the given scenarios with fseek(), assume

file pointer is at 10 th character and there are 100 characters in the file.

(a) Set file pointer to the last character in the file.

(b) Set file pointer to the beginning of the file.

 

 

Role of fseek() in C

The fseek() function is used to move the file pointer to a specific position in a file. It is commonly used in random access files to read or write data at any location.

Syntax

fseek(FILE *fp, long int offset, int position);

Parameters

  • fp → File pointer
  • offset → Number of bytes to move
  • position → Starting point for movement

Reference Positions

Constant Meaning
SEEK_SET Beginning of file
SEEK_CUR Current position
SEEK_END End of file

 

Given

  • File pointer currently at 10th character
  • Total characters in file = 100

 

(a) Set file pointer to the last character in the file

The last character is 100th character.
From the end of the file, we move 1 character backward.

fseek(fp, -1, SEEK_END);

This positions the file pointer at the last character.

 

(b) Set file pointer to the beginning of the file

Move the pointer to the start of the file.

fseek(fp, 0, SEEK_SET);

This places the file pointer at the first character of the file.

 

Summary

Task Statement
Move to last character fseek(fp, -1, SEEK_END);
Move to beginning fseek(fp, 0, SEEK_SET);

Thus, fseek() allows flexible positioning of the file pointer for random access in files.