Unit-5
File Management
In C, files are used to store data permanently. You can perform file operations like read, write, and
modify data stored in files.
Types of Files-
1. Text Files (.txt)
o Stores data in readable format.
o Opened in text mode.
2. Binary Files (.bin)
o Stores data in binary form.
o Faster and more space-efficient.
Basic File I/O Operations-
Functions from <stdio.h>:
Operation Function
Open a file fopen()
Close a file fclose()
Read from file fscanf(), fgets(), fgetc()
Write to file fprintf(), fputs(), fputc()
Check end of file feof()
File Opening Modes-
Mode Meaning
"r" Open for reading
"w" Open for writing (creates new or clears existing file)
"a" Open for appending
"r+" Open for both reading and writing
"w+" Open for reading and writing (clears existing file)
"a+" Open for reading and appending
Random Access to Files- In C, random access to files means moving the file pointer to any
desired position in a file to read or write data directly from that position — without having to
sequentially process the file from the beginning.
This is especially useful when dealing with large files or files with records of fixed size (like in
simple database systems).
fseek() – Move file pointer to a specific position
ftell() – Get current file pointer position
rewind() – Move file pointer to beginning
//Program to Write and Read from a Text File-
#include<stdio.h>
void main() {
FILE *fp;
char str[50];
clrscr();
fp = fopen("[Link]", "w"); // open for writing
fprintf(fp, "Hello, File Handling in C!");
fclose(fp);
fp = fopen("[Link]", "r"); // open for reading
fgets(str, 50, fp);
printf("File Content: %s\n", str);
fclose(fp);
getch();
//Random Access Example: Read 5th Character in File-
#include<stdio.h>
void main() {
FILE *fp;
char ch;
fp = fopen("[Link]", "r");
if(fp == NULL)
printf("File not found.\n");
//return 0;
fseek(fp, 4, SEEK_SET); // move to 5th character (index starts at 0)
ch = fgetc(fp);
printf("5th Character: %c\n", ch);
fclose(fp);
getch();
Dynamic Memory Allocation-
Dynamic memory allocation in C means allocating memory at runtime using pointers, instead
of defining memory size during compile time (like with arrays). It allows a program to request
memory from the system as needed while the program is running, and to release it when done.
This is done using functions from the <stdlib.h> library.
Functions for Dynamic Memory Allocation-
Function Purpose
malloc() (Memory Allocates a specified number of bytes, returns void * pointer.
Allocation) Memory content is uninitialized.
calloc() (Contiguous Allocates memory for an array of elements and initializes all bytes to
Allocation) zero.
realloc() (ReAllocation) Changes the size of previously allocated memory block.
Function Purpose
free() (Free Memory) Releases dynamically allocated memory.
Program for DMA-
#include <stdio.h>
#include <stdlib.h>
void main() {
int *ptr;
int n, i;
clrscr();
printf("Enter number of elements: ");
scanf("%d", &n);
// Dynamic memory allocation using malloc
ptr = (int *) malloc(n * sizeof(int));
if (ptr == NULL) {
printf("Memory allocation failed!\n");
return 1;
// Storing values
printf("Enter elements:\n");
for (i = 0; i < n; i++) {
scanf("%d", &ptr[i]);
// Displaying values
printf("You entered:\n");
for (i = 0; i < n; i++) {
printf("%d ", ptr[i]);
// Free allocated memory
free(ptr);
getch();