Exercise
File comparer
Objetive
Create a C# program to tell if two files (of any kind) are identical (have the same content).
Example Code
// Import necessary namespaces for file handling
using System; // Basic input/output operations
using System.IO; // FileStream for reading files
using System.Text; // StringBuilder for efficient string handling
class FileComparer // Main class for the file comparison program
{
static void Main(string[] args) // Entry point of the program
{
// Check if the correct number of arguments (input files) are provided
if (args.Length != 2) // If not enough arguments are provided
{
Console.WriteLine("Usage: FileComparer "); // Show usage instructions
return; // Exit the program if the arguments are incorrect
}
string file1 = args[0]; // The first file name to compare
string file2 = args[1]; // The second file name to compare
// Check if both files exist
if (!File.Exists(file1)) // If the first file does not exist
{
Console.WriteLine($"Error: The file {file1} does not exist."); // Inform the user about the missing file
return; // Exit the program if the first file is missing
}
if (!File.Exists(file2)) // If the second file does not exist
{
Console.WriteLine($"Error: The file {file2} does not exist."); // Inform the user about the missing file
return; // Exit the program if the second file is missing
}
try
{
// Open both files for reading using FileStream
using (FileStream fs1 = new FileStream(file1, FileMode.Open, FileAccess.Read)) // Open the first file
using (FileStream fs2 = new FileStream(file2, FileMode.Open, FileAccess.Read)) // Open the second file
{
// Compare file lengths first
if (fs1.Length != fs2.Length) // If the file sizes are different
{
Console.WriteLine("The files are different (different sizes)."); // Inform the user about different sizes
return; // Exit the program if the sizes don't match
}
// Use byte arrays to read the files in chunks
byte[] buffer1 = new byte[1024]; // Buffer to hold data from the first file
byte[] buffer2 = new byte[1024]; // Buffer to hold data from the second file
int bytesRead1, bytesRead2; // Variables to store the number of bytes read from each file
// Compare the files byte by byte
while ((bytesRead1 = fs1.Read(buffer1, 0, buffer1.Length)) > 0) // Read from the first file
{
bytesRead2 = fs2.Read(buffer2, 0, buffer2.Length); // Read from the second file
// If the number of bytes read is different, the files are different
if (bytesRead1 != bytesRead2)
{
Console.WriteLine("The files are different (different lengths in chunks)."); // Inform the user about the discrepancy
return; // Exit the program if the chunk sizes don't match
}
// Compare the bytes from both buffers
for (int i = 0; i < bytesRead1; i++) // Loop through the bytes read
{
if (buffer1[i] != buffer2[i]) // If a byte doesn't match
{
Console.WriteLine("The files are different (mismatch found)."); // Inform the user about the mismatch
return; // Exit the program if any byte doesn't match
}
}
}
// If all checks pass, the files are identical
Console.WriteLine("The files are identical."); // Inform the user that the files are the same
}
}
catch (Exception ex) // Catch any exceptions that occur during file reading or comparison
{
Console.WriteLine($"An error occurred: {ex.Message}"); // Display the error message
}
}
}