Need help on C++. Write a code with the following task. Database file provided.
ID: 3803209 • Letter: N
Question
Need help on C++. Write a code with the following task. Database file provided.
//Prompt the user for the database file name. This file name is the path and the file name. Do not assume the file is in a particular directory. Use a single prompt.
//Open the database
//Read in the records using a custom class to hold a record
//Remove the duplicates
//Write the scrubbed database as a new CSV file starting with 'new_' and the input database file name.
//Write the new file to the same directory that the database file was read from.
Suggestion: Create a class to hold a single data record.
Override the == operator to ease comparison and override the << operator to output the comma separated values.
Use the class constructor to receive a line from the database then parse the values or override the >> input operator.
Modify the string stream getline() delimiter to a comma.
Duplicated.csv
1,A,13107-070,257,2.71
2,B,66389-0001,948,94.43
3,C,53808-0776,278,65.75
4,D,41250-416, 203,36.95
5,E,64058-413,783,45.65
6,F,64092-113,450,55.23
7,C,53808-0776,278,65.75
8,G,0268-1154,976,65.17
9,E,64058-413,783,45.65
10,Z,68788-9852,362,56.57
Explanation / Answer
sample code:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <map>
using namespace std;
using std::string;
typedef map<string, int> line_record;
std::string splitFilename (const string& str, int type);
int main (int argc, const char * argv[]) {
string line, filePath, fileName, newFileName, tempName;
line_record lines;
int line_number = 1;
cout << "Enter file name";
cin>>fileName;
ifstream infile (fileName.c_str());
ofstream outputFile;
tempName = splitFilename(fileName.c_str(), 1);
newFileName = "new_"+tempName;
filePath = splitFilename(fileName.c_str(), 2);
if(filePath != "")
filePath = filePath+"/"+newFileName;
else
filePath = newFileName;
outputFile.open(filePath.c_str());
if (infile.is_open()) {
while (std::getline(infile, line)) {
line_record::iterator existing = lines.find(line);
if (existing != lines.end()) // if it was already in the map
existing->second = -1; // indicate that it's duplicated
else
lines.insert(make_pair(line, line_number)); // otherwise, add it to map
++line_number;
}
for(line_record::const_iterator it = lines.begin(); it != lines.end(); ++it)
{
cout << it->first << " ";
outputFile << it->first << endl;
}
infile.close();
outputFile.close();
}else {
cout << "Cannot open";
}
return 0;
}
std::string splitFilename (const string& str, int type)
{
int found=0; string fName, fPath;
found=str.find_last_of("/\");
fPath = str.substr(0,found);
fName = str.substr(found+1);
if(type == 1)
return fName; //file name
else if(found>0)
return fPath; //file path
else
return "";
}
../dublicated.csv
A,13107-070,257,2.71
B,66389-0001,948,94.43
C,53808-0776,278,65.75
D,41250-416, 203,36.95
E,64058-413,783,45.65
F,64092-113,450,55.23
C,53808-0776,278,65.75
G,0268-1154,976,65.17
E,64058-413,783,45.65
Z,68788-9852,362,56.57
Output file:
../new_dublicated.csv
A,13107-070,257,2.71
B,66389-0001,948,94.43
C,53808-0776,278,65.75
D,41250-416, 203,36.95
E,64058-413,783,45.65
F,64092-113,450,55.23
G,0268-1154,976,65.17
Z,68788-9852,362,56.57
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.