5.6k views
0 votes
Write a 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.

User Mattl
by
5.4k points

1 Answer

0 votes

Answer:

The program in cpp for the given scenario is shown below.

#include <stdio.h>

#include <iostream>

#include <fstream>

#include <queue>

using namespace std;

int main()

{

//object created of file stream

ofstream out, out1;

//file opened for writing

out.open("words.txt");

out1.open("word.txt");

//queues declared for two files

queue<string> first, second;

//string variables declared to read from file

string s1, s2;

//object created of file stream

ifstream one("words.txt");

ifstream two("word.txt");

//first file read into the que

while (getline(one, s1)) {

first.push(s1);

}

//second file read into the queue

while (getline(two, s2)) {

second.push(s2);

}

//both files compared

if(first!=second)

cout<<"files are not the same"<<endl;

else

cout<<"files are identical"<<endl;

//file closed

out.close();

return 0;

}

OUTPUT:

files are identical

Step-by-step explanation:

1. The object of the file output stream are created for both the files.

2. The two files are opened using the objects created in step 1.

3. The objects of the file input stream are created for both the files.

4. The queues are declared for both the files.

5. Two string variables are declared.

6. Inside a while loop, the text from the first is read into the string variable. The value of this string variable is then inserted into the queue.

7. The loop continues till the string is read and end of file is not reached.

8. Inside another while loop, the text from the second file is read into the second string variable and this string is inserted into another queue.

9. The loop continues till the end of file is not reached.

10. Using if-else statement, both the queues are compared.

11. If any character in the first queue does not matches the corresponding character of the second queue, the message is displayed accordingly.

12. Alternatively, if the contents of both the queues match, the message is displayed accordingly.

13. In the given example program, the message is displayed as "files are identical." This is because both the files are empty and the respective queues are considered identical since both the queues are empty.

14. Since queues are used, the queue header file is included in the program.

15. An integer value 0 is returned to indicate successful execution of the program.

User Spaetzel
by
5.1k points