Practical / Solution
File Handling: File Opening Modes — Exercise 1
Problem
Write a C program to create a file and write text into it using the
w mode.
Program
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fprintf(fp, "BCA Study Portal\n");
fclose(fp);
printf("File created and data written successfully.\n");
return 0;
}
Explanation
The w mode opens a file for writing. If the file does not
exist, it can be created. Existing contents are replaced when the file
is opened for writing.
Expected Output
File created and data written successfully.
File Handling: File Opening Modes — Exercise 2
Problem
Write a C program to open an existing file in r mode and
read its contents character by character.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("student.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
printf("File contents:\n");
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
return 0;
}
Explanation
The r mode opens an existing file for reading.
fgetc() reads one character at a time until
EOF is reached.
Expected Output
File contents:
BCA Study Portal
File Handling: File Opening Modes — Exercise 3
Problem
Write a C program to append new text to an existing file using the
a mode.
Program
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("student.txt", "a");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fprintf(fp, "C Programming Notes\n");
fclose(fp);
printf("Data appended successfully.\n");
return 0;
}
Explanation
The a mode opens a file for appending. New data is written
at the end of the existing file instead of replacing its contents.
Expected Output
Data appended successfully.
File Handling: File Opening Modes — Exercise 4
Problem
Write a C program to use the r+ mode to read from and
write to an existing file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("student.txt", "r+");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
printf("Existing content:\n");
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
fprintf(fp, "New Line\n");
fclose(fp);
printf("\nData processing completed.\n");
return 0;
}
Explanation
The r+ mode allows both reading and writing on an existing
file. The file must already exist. The exact effect of writing also
depends on the current file position.
Expected Output
Existing content:
BCA Study Portal
Data processing completed.
File Handling: File Opening Modes — Exercise 5
Problem
Write a C program to create a new file for both reading and writing
using the w+ mode.
Program
#include <stdio.h>
int main()
{
FILE *fp;
char text[100];
fp = fopen("data.txt", "w+");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fprintf(fp, "Welcome to File Handling.");
rewind(fp);
fgets(text, sizeof(text), fp);
printf("File content = %s\n", text);
fclose(fp);
return 0;
}
Explanation
The w+ mode opens a file for both reading and writing.
The file can be created if it does not exist, while existing contents
are discarded when the file is opened.
Expected Output
File content = Welcome to File Handling.
File Handling: File Opening Modes — Exercise 6
Problem
Write a C program to use the a+ mode to append data to a
file and then read the file contents.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("notes.txt", "a+");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fprintf(fp, "Pointers are important in C.\n");
rewind(fp);
printf("File contents:\n");
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
return 0;
}
Explanation
The a+ mode allows both reading and appending. New data
is added at the end of the file, while reading can be performed after
repositioning the file position as needed.
Expected Output
File contents:
Pointers are important in C.
File Handling: File Opening Modes — Exercise 7
Problem
Write a C program to check whether a file can be opened using the
r mode before reading it.
Program
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("record.txt", "r");
if (fp == NULL)
{
printf("File does not exist or cannot be opened.\n");
}
else
{
printf("File opened successfully.\n");
fclose(fp);
}
return 0;
}
Explanation
fopen() returns NULL when the file cannot be
opened successfully. Checking this result prevents the program from
trying to use an invalid file pointer.
Expected Output
File opened successfully.
If the file does not exist, the program displays an appropriate
error message instead.
File Handling: File Opening Modes — Exercise 8
Problem
Write a C program to demonstrate the use of the w mode
for storing student information in a file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int roll;
char name[50];
float marks;
fp = fopen("student_record.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
printf("Enter roll number: ");
scanf("%d", &roll);
printf("Enter name: ");
scanf("%49s", name);
printf("Enter marks: ");
scanf("%f", &marks);
fprintf(fp,
"Roll = %d\nName = %s\nMarks = %.2f\n",
roll,
name,
marks);
fclose(fp);
printf("\nStudent record saved successfully.\n");
return 0;
}
Explanation
The w mode is useful when a program needs to create a new
file or replace the contents of an existing file with fresh data.
Expected Output
Enter roll number: 101
Enter name: Rahul
Enter marks: 88
Student record saved successfully.
File Handling: File Opening Modes — Exercise 9
Problem
Write a C program to append multiple student names to a file using
the a mode.
Program
#include <stdio.h>
int main()
{
FILE *fp;
char name[50];
int i;
fp = fopen("students.txt", "a");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
for (i = 0; i < 3; i++)
{
printf("Enter student name: ");
scanf("%49s", name);
fprintf(fp, "%s\n", name);
}
fclose(fp);
printf("Student names appended successfully.\n");
return 0;
}
Explanation
The program opens the file in append mode so that newly entered names
are added after the existing data. Existing contents are preserved.
Expected Output
Enter student name: Rahul
Enter student name: Priya
Enter student name: Amit
Student names appended successfully.
File Handling: File Opening Modes — Exercise 10
Problem
Write a C program that demonstrates choosing a file opening mode
using a menu and performs the corresponding file operation.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int choice;
printf("1. Write file\n");
printf("2. Read file\n");
printf("3. Append file\n");
printf("Enter choice: ");
scanf("%d", &choice);
if (choice == 1)
{
fp = fopen("menu.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fprintf(fp, "Data written using w mode.\n");
fclose(fp);
printf("Write operation completed.\n");
}
else if (choice == 2)
{
int ch;
fp = fopen("menu.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
printf("File contents:\n");
while ((ch = fgetc(fp)) != EOF)
{
putchar(ch);
}
fclose(fp);
}
else if (choice == 3)
{
fp = fopen("menu.txt", "a");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fprintf(fp, "Data appended using a mode.\n");
fclose(fp);
printf("Append operation completed.\n");
}
else
{
printf("Invalid choice.\n");
}
return 0;
}
Explanation
This example combines the commonly used text-file opening modes.
The program selects w, r or a
according to the user's choice and then performs the corresponding
operation.
Expected Output
1. Write file
2. Read file
3. Append file
Enter choice: 1
Write operation completed.