
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
C program to compare two files and report mismatches
In the C programming language, the programmer can excess the files and read and write content in them.
A file is a simple memory block that can store information, here we are concerned with text only.
In this program, we will compare two files and report mismatches that occur. These files are almost identical but may have some characters that are different. Also, the program will return the line and position of the file at which the first mismatch occurs.
Algorithm
Step 1: Open both the file with pointer at the starting. Step 2: Fetch data from file as characters one by one. Step 3: Compare the characters. If the characters are different then return the line and position of the error character.
Example
#include<stdio.h> #include<string.h> #include<stdlib.h> void compareFiles(FILE *file1, FILE *file2){ char ch1 = getc(file1); char ch2 = getc(file2); int error = 0, pos = 0, line = 1; while (ch1 != EOF && ch2 != EOF){ pos++; if (ch1 == '
' && ch2 == '
'){ line++; pos = 0; } if (ch1 != ch2){ error++; printf("Line Number : %d \tError" " Position : %d
", line, pos); } ch1 = getc(fp1); ch2 = getc(fp2); } printf("Total Errors : %d\t", error); } int main(){ FILE *file1 = fopen("file1.txt", "r"); FILE *file2 = fopen("file2.txt", "r"); if (file1 == NULL || file2 == NULL){ printf("Error : Files not open"); exit(0); } compareFiles(file1, file2); fclose(file1); fclose(file2); return 0; }
Output
// content of the files File1 : Hello! Welcome to tutorials Point File2: Hello! Welcome to turoials point Line number: 2 Error position: 15 Total error : 1
- Related Articles
- Program to compare two fractions in C
- C++ Program to Compare two strings lexicographically
- How to compare two different files line by line in Python?
- Compare Two Different Files Line by Line in Java
- Java Program to compare two sets
- Java Program to Compare Two Strings
- PHP program to compare two dates
- Golang program to compare two strings
- How to compare files in Python
- Java Program to compare two Boolean Arrays
- Java Program to compare two Byte Arrays
- Python program to compare two Pandas series
- Program to Compare two strings in Java
- Java Program to Compare two strings lexicographically
- Golang program to compare two strings lexicographically

Advertisements