Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

File Compare C++

File Compare
Write a C++ program that opens two text files and reads their contents into two separate
queues. The program should then determine whether the files are identical by comparing
the characters in the queues. When two nonidentical characters are encountered,
the program should display a message indicating that the files are not the same. If
both queues contain the same set of characters, a message should be displayed indicating
that the files are identical.

Answer:’



//  File Compare
#include<iostream>
#include<fstream>
#include<cstdlib>
#include "Dynqueue.h"
using namespace std;

int main()
{
	// Create an input file stream object.
	fstream file1("file1.txt", ios::in);

	// Create an output file stream object.
	fstream file2("file2.txt", ios::out);

	// Create two queues to hold characters.
	Dynque<char> queue1;
	Dynque<char> queue2;
	char ch1, ch2;

	// Read all the characters from file #1
	// and enqueue them in queue1.
	file1.get(ch1);
	while (!file1.eof())
	{
		queue1.enqueue(ch1);
		file1.get(ch1);
	}

	// Read all the characters from file #2
	// and enqueue them in queue2.
	file2.get(ch2);
	while (!file2.eof())
	{
		queue2.enqueue(ch2);
		file2.get(ch2);
	}

	// Close the files.
	file1.close();
	file2.close();

	// Dequeue the characters from each queue
	// one-at-a-time and compare them.
	while (!queue1.isEmpty() && !queue2.isEmpty())
	{
		queue1.dequeue(ch1);
		queue2.dequeue(ch2);
		cout << ch1 << "\t" << ch2 << endl;
		if (ch1 != ch2)
		{
			cout << "\nThe files are not identical.\n";
			return 0;
		}
	}

	cout << "\nThe files are identical.\n" << endl << endl;
	return 0;

}



// Dynqueue class template

template <class T>
class Dynque
{
	private:
		struct QueueNode
		{
			T value;
			QueueNode *next;
		};
	
		QueueNode *front;
		QueueNode *rear;
		int numItems;
	public:
	 Dynque();
	 ~Dynque();
	 void enqueue(T);
	 void dequeue(T &);
	 bool isEmpty();
	 bool isFull();
	 void clear();
};

//************************************************
// Constructor                                   *
//************************************************

template <class T>
Dynque<T>::Dynque()
{
	front = NULL;
	rear = NULL;
	numItems = 0;
}

//************************************************
// Destructor                                    *
//************************************************

template <class T>
Dynque<T>::~Dynque()
{
	clear();
}

//************************************************
// Function enqueue inserts the value in num     *
// at the rear of the queue.                     *
//************************************************

template <class T>
void Dynque<T>::enqueue(T num)
{
	QueueNode *newNode;

	newNode = new QueueNode;
	newNode->value = num;
	newNode->next = NULL;
	if (isEmpty())
		front = rear = newNode;
	else
	{
		rear->next = newNode;
		rear = newNode;
	}
	numItems++;
}

//************************************************
// Function dequeue removes the value at the     *
// front of the queue, and copies it into num.   *
//************************************************

template <class T>
void Dynque<T>::dequeue(T &num)
{
	QueueNode *temp;

	if (isEmpty())
		cout << "The queue is empty.\n"; else { num = front->value;
		temp = front->next;
		delete front;
		front = temp;
		numItems--;
	}
}

//************************************************
// Function isEmpty returns true if the queue    *
// is empty, and false otherwise.                *
//************************************************

template <class T>
bool Dynque<T>::isEmpty()
{
	if (numItems)
		return false;
	else
		return true;
}

//************************************************
// Function clear dequeues all the elements      *
// in the queue.                                 *
//************************************************

template <class T>
void Dynque<T>::clear()
{
	T value;			// Dummy variable for dequeue

	while(!isEmpty())
		dequeue(value);
}

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!