Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program to read records in a plain text file, then remove duplicated

ID: 3880574 • Letter: W

Question

Write a C++ program to read records in a plain text file, then remove duplicated records, and write unique ones to a plain text file. You can use any algorithm and data structures that you prefer, as long as the results are correct. It is preferred, but not necessary, that your algorithm is as efficient as possible, both in processing time as well as memory management.

Input and Output Specification:

The input is one text file with 0 - 10000 records. The content of each record is confined to a pair of {} and doesn’t contain ’{’ or ’}’, but may have other symbols like space, comma, colon, single or double quote, and so on. In the input and output file, each line ends in a ’ ’ character. One record may not be necessary in one line in the input file. But you should output each unique record in one line without any spaces. The output records can be in any orders. It doesn’t matter if you have or don’t have one empty line at the end of the output file.

Example of input files (between the lines)

inputl.txt id: 1234567,first:Mary, last:Green) [id: 1234568, first:Peter, last :Morgan) id: 1234567, first : Mary, last:Green) input2.txt (id: 1234567, first:Mary, last:Green, GPA: 4.0 id: 1234568, first:Peter, last:White GPA:3.8) id: 1234567, first : Mary, last:Green, GPA:3.91 output1.txt id: 1234567,first:Mary, last:Green) fid: 1234568, first:Peter, last : Morgan) output2.txt

Explanation / Answer

Hi... I have written c++ file to read the data from file to string array and remove duplicates in that array. finally output writes to an output file. Please check below code.

input.txt

Ha i ii
Santhosh
Haiii

Main.cpp


#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
string* line=new string[3]; //input file has 3 lines
ifstream myfile ("input.txt");
int count=0;
if (myfile.is_open())
{
while ( getline (myfile,line[count]) )
{
cout << line[count] << ' ';
count++;
}
myfile.close();
}else cout << "Unable to open file";
  
// removing spaces from words
for(int g=0;g<count;g++){
string str = line[g];
int i = 0, j = 0;
while (str[i])
{
if (str[i] != ' ')
str[j++] = str[i];
i++;
}
str[j] = '';
line[g] = str;
  
}

// removing duplicates from string array
int n=2;
for(int i=0;i<n;++i)
for(int j=i+1;j<n;)
{
if(line[i]==line[j])
{
for(int k=j;k<n-1;++k)
line[k]=line[k+1];
  
--n;
}
else
++j;
}
  
// write result into output file
ofstream myfile1;
myfile1.open ("output.txt");
for(int i=0;i<n;i++){
myfile1<<line[i]<<" ";

}
  
myfile1.close();
  

return 0;
}

Output to an text file.

output.txt

Haiii

Santhosh  

Please check the code and let me know any issues. Thank you. All the best.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote