Practical / Solution
Reading from Files — Exercise 1
Problem
Write a C program to open a text file in read mode and display its
contents character by character using fgetc().
Program
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
fp = fopen("data.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 fgetc() function reads one character at a time from
the file. Reading continues until the end-of-file indicator
EOF is reached.
Expected Output
File contents:
Welcome to C Programming.
Reading from Files — Exercise 2
Problem
Write a C program to read a line from a text file using
fgets() and display it.
Program
#include <stdio.h>
int main()
{
FILE *fp;
char line[100];
fp = fopen("data.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
if (fgets(line, sizeof(line), fp) != NULL)
{
printf("First line:\n");
printf("%s", line);
}
fclose(fp);
return 0;
}
Explanation
The fgets() function reads a line or a specified maximum
number of characters from a file. It is useful for line-based text reading.
Expected Output
First line:
Welcome to C Programming.
Reading from Files — Exercise 3
Problem
Write a C program to read a file line by line using fgets().
Program
#include <stdio.h>
int main()
{
FILE *fp;
char line[100];
fp = fopen("notes.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
printf("File contents:\n");
while (fgets(line, sizeof(line), fp) != NULL)
{
printf("%s", line);
}
fclose(fp);
return 0;
}
Explanation
The loop repeatedly calls fgets() until it returns
NULL, allowing the complete text file to be read one
line at a time.
Expected Output
File contents:
C Programming
Pointers
Structures
Reading from Files — Exercise 4
Problem
Write a C program to read integer values from a text file using
fscanf() and calculate their sum.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int number;
int sum = 0;
fp = fopen("numbers.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
while (fscanf(fp, "%d", &number) == 1)
{
sum += number;
}
fclose(fp);
printf("Sum = %d\n", sum);
return 0;
}
Explanation
fscanf() reads formatted data from a file. Here, each
integer is read and added to sum until no more integers
can be read.
Expected Output
The file contains:
10 20 30 40 50
Sum = 150
Reading from Files — Exercise 5
Problem
Write a C program to count the number of characters present in a text
file using fgetc().
Program
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
int count = 0;
fp = fopen("data.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
count++;
}
fclose(fp);
printf("Number of characters = %d\n", count);
return 0;
}
Explanation
Every successful call to fgetc() returns one character.
The program increments a counter for each character until
EOF is encountered.
Expected Output
If the file contains:
Hello
Number of characters = 5
Reading from Files — Exercise 6
Problem
Write a C program to count the number of lines in a text file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int ch;
int lines = 0;
fp = fopen("notes.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
while ((ch = fgetc(fp)) != EOF)
{
if (ch == '
')
{
lines++;
}
}
fclose(fp);
printf("Number of lines = %d\n", lines);
return 0;
}
Explanation
Each newline character represents the end of a line. The program counts
newline characters while reading the file.
Expected Output
If the file contains 4 lines,
Number of lines = 4
Reading from Files — Exercise 7
Problem
Write a C program to read student records containing roll number,
name and marks from a file using fscanf().
Program
#include <stdio.h>
int main()
{
FILE *fp;
int roll;
char name[50];
float marks;
fp = fopen("students.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
printf("Student Records:\n");
while (fscanf(fp, "%d %49s %f",
&roll,
name,
&marks) == 3)
{
printf("Roll = %d\n", roll);
printf("Name = %s\n", name);
printf("Marks = %.2f\n", marks);
printf("------------------\n");
}
fclose(fp);
return 0;
}
Explanation
Formatted records can be read from a text file using
fscanf(). The loop continues only when all three expected
values are successfully read.
Expected Output
Student Records:
Roll = 101
Name = Rahul
Marks = 88.00
------------------
Roll = 102
Name = Priya
Marks = 92.00
Reading from Files — Exercise 8
Problem
Write a C program to search for a particular word in a text file
using line-by-line reading.
Program
#include <stdio.h>
#include <string.h>
int main()
{
FILE *fp;
char line[200];
char word[50];
int found = 0;
fp = fopen("notes.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
printf("Enter word to search: ");
scanf("%49s", word);
while (fgets(line, sizeof(line), fp) != NULL)
{
if (strstr(line, word) != NULL)
{
found = 1;
break;
}
}
fclose(fp);
if (found)
printf("Word found in file.\n");
else
printf("Word not found.\n");
return 0;
}
Explanation
The file is read line by line and strstr() checks whether
the searched word occurs within the current line.
Expected Output
Enter word to search: pointer
Word found in file.
Reading from Files — Exercise 9
Problem
Write a C program to find the largest integer stored in a text file.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int number;
int largest;
fp = fopen("numbers.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
if (fscanf(fp, "%d", &largest) != 1)
{
printf("No numbers found in file.\n");
fclose(fp);
return 0;
}
while (fscanf(fp, "%d", &number) == 1)
{
if (number > largest)
{
largest = number;
}
}
fclose(fp);
printf("Largest number = %d\n", largest);
return 0;
}
Explanation
The first number is used as the initial largest value. Every remaining
number is then compared with it, and the largest value is updated when
necessary.
Expected Output
The file contains:
25 80 15 92 47
Largest number = 92
Reading from Files — Exercise 10
Problem
Write a C program to read a student result file and calculate the
average marks of all students stored in it.
Program
#include <stdio.h>
int main()
{
FILE *fp;
int roll;
char name[50];
float marks;
float total = 0;
float average;
int count = 0;
fp = fopen("results.txt", "r");
if (fp == NULL)
{
printf("File could not be opened.\n");
return 1;
}
while (fscanf(fp, "%d %49s %f",
&roll,
name,
&marks) == 3)
{
total += marks;
count++;
}
fclose(fp);
if (count == 0)
{
printf("No records found.\n");
return 0;
}
average = total / count;
printf("Number of students = %d\n", count);
printf("Average marks = %.2f\n", average);
return 0;
}
Explanation
The program reads each student record from the file, adds the marks
to total, counts the records and finally calculates the
average marks.
Expected Output
If the file contains 3 student records with marks 80, 90 and 85:
Number of students = 3
Average marks = 85.00