Practical / Solution
Writing from Files — Exercise 1
Problem
Write a C program to create a text file and write a simple message
into it using fprintf().
Program
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("message.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fprintf(fp, "Welcome to BCA Study Portal.\n");
fclose(fp);
printf("Data written successfully.\n");
return 0;
}
Explanation
The file is opened in w mode and fprintf()
writes formatted text into it. Finally, fclose() closes
the file.
Expected Output
Data written successfully.
Writing from Files — Exercise 2
Problem
Write a C program to take a student's name and roll number from the
user and save the information into a file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int roll;
char name[50];
fp = fopen("student.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);
fprintf(fp, "Roll Number: %d\n", roll);
fprintf(fp, "Name: %s\n", name);
fclose(fp);
printf("Student record saved successfully.\n");
return 0;
}
Explanation
The program accepts student details and uses fprintf()
to write formatted information into the file.
Expected Output
Enter roll number: 101
Enter name: Rahul
Student record saved successfully.
Writing from Files — Exercise 3
Problem
Write a C program to write multiple numbers into a file using a loop.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int i;
fp = fopen("numbers.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
for (i = 1; i <= 10; i++)
{
fprintf(fp, "%d\n", i);
}
fclose(fp);
printf("Numbers written successfully.\n");
return 0;
}
Explanation
The loop generates numbers from 1 to 10, and each number is written
to a new line in the file using fprintf().
Expected Output
Numbers written successfully.
File contains:
1
2
3
...
10
Writing from Files — Exercise 4
Problem
Write a C program to store five integer values entered by the user
into a file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int number;
int i;
fp = fopen("values.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
printf("Enter 5 numbers:\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &number);
fprintf(fp, "%d\n", number);
}
fclose(fp);
printf("Values stored successfully.\n");
return 0;
}
Explanation
Each input value is immediately written to the file. The loop repeats
five times to store all five numbers.
Expected Output
Enter 5 numbers:
10 20 30 40 50
Values stored successfully.
Writing from Files — Exercise 5
Problem
Write a C program to store the multiplication table of a number
in a text file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int number;
int i;
fp = fopen("table.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
printf("Enter a number: ");
scanf("%d", &number);
for (i = 1; i <= 10; i++)
{
fprintf(fp,
"%d x %d = %d\n",
number,
i,
number * i);
}
fclose(fp);
printf("Multiplication table saved successfully.\n");
return 0;
}
Explanation
The program generates the multiplication table using a loop and writes
each line into the file with fprintf().
Expected Output
Enter a number: 5
Multiplication table saved successfully.
File contains:
5 x 1 = 5
5 x 2 = 10
...
5 x 10 = 50
Writing from Files — Exercise 6
Problem
Write a C program to save employee details such as ID, name and
salary into a file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int id;
char name[50];
float salary;
fp = fopen("employee.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
printf("Enter employee ID: ");
scanf("%d", &id);
printf("Enter name: ");
scanf("%49s", name);
printf("Enter salary: ");
scanf("%f", &salary);
fprintf(fp, "Employee ID: %d\n", id);
fprintf(fp, "Name: %s\n", name);
fprintf(fp, "Salary: %.2f\n", salary);
fclose(fp);
printf("Employee record saved successfully.\n");
return 0;
}
Explanation
Multiple data types can be written to a text file using formatted
output. Each value is written with a suitable format specifier.
Expected Output
Enter employee ID: 501
Enter name: Amit
Enter salary: 45000
Employee record saved successfully.
Writing from Files — Exercise 7
Problem
Write a C program to write the first 10 even numbers into a file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int i;
fp = fopen("even.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
for (i = 1; i <= 10; i++)
{
fprintf(fp, "%d\n", i * 2);
}
fclose(fp);
printf("Even numbers written successfully.\n");
return 0;
}
Explanation
The expression i * 2 generates even numbers. Each result
is written to a separate line in the file.
Expected Output
Even numbers written successfully.
File contains:
2 4 6 8 10 12 14 16 18 20
Writing from Files — Exercise 8
Problem
Write a C program to append a new line of text to an existing file
using fputs().
Program
#include <stdio.h>
int main()
{
FILE *fp;
fp = fopen("notes.txt", "a");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
fputs("New C Programming topic added.\n", fp);
fclose(fp);
printf("New data appended successfully.\n");
return 0;
}
Explanation
The fputs() function writes a string to a file.
Opening the file with a mode places the new text at the
end of the existing contents.
Expected Output
New data appended successfully.
Writing from Files — Exercise 9
Problem
Write a C program to store the marks of five students in a file and
calculate the total marks while writing them.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int marks;
int total = 0;
int i;
fp = fopen("marks.txt", "w");
if (fp == NULL)
{
printf("Unable to open file.\n");
return 1;
}
printf("Enter marks of 5 students:\n");
for (i = 0; i < 5; i++)
{
scanf("%d", &marks);
fprintf(fp, "%d\n", marks);
total += marks;
}
fclose(fp);
printf("Marks saved successfully.\n");
printf("Total marks = %d\n", total);
return 0;
}
Explanation
Every entered mark is written to the file. At the same time, the
program adds the values to calculate their total.
Expected Output
Enter marks of 5 students:
80 75 90 85 70
Marks saved successfully.
Total marks = 400
Writing from Files — Exercise 10
Problem
Write a C program to create a complete student result file containing
roll number, name, marks, total and average for a student.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int roll;
char name[50];
int math;
int cProgramming;
int computer;
int total;
float average;
fp = fopen("result.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 in Mathematics, C Programming and Computer: ");
scanf("%d %d %d",
&math,
&cProgramming,
&computer);
total = math + cProgramming + computer;
average = total / 3.0;
fprintf(fp, "=============================\n");
fprintf(fp, " STUDENT RESULT\n");
fprintf(fp, "=============================\n");
fprintf(fp, "Roll : %d\n", roll);
fprintf(fp, "Name : %s\n", name);
fprintf(fp, "Math : %d\n", math);
fprintf(fp, "C : %d\n", cProgramming);
fprintf(fp, "Computer : %d\n", computer);
fprintf(fp, "Total : %d\n", total);
fprintf(fp, "Average : %.2f\n", average);
fclose(fp);
printf("\nStudent result saved successfully.\n");
return 0;
}
Explanation
This final example combines user input, arithmetic operations and
formatted file writing. The complete student result is stored in
result.txt using fprintf().
Expected Output
Enter roll number: 101
Enter name: Priya
Enter marks in Mathematics, C Programming and Computer: 90 88 92
Student result saved successfully.
File contains:
=============================
STUDENT RESULT
=============================
Roll : 101
Name : Priya
Math : 90
C : 88
Computer : 92
Total : 270
Average : 90.00