repl.it
@SPQR/

CSV Reader and String Comparer

C++11

Just something i made to help with another project.

fork
loading
Files
  • main.cpp
  • data_out.txt
  • data_unused.txt
  • data.csv
  • data.txt
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream> //console i/o
#include <vector> //std::vector
#include <string> //std::to_string
#include <fstream> //file i/o
#include <sstream> //std::stringstream
#include <utility> //std::pair

//This program gets sets of (string, int) 

std::vector<std::string> getNextLine(std::istream& istr) {
    std::vector<std::string> output;
    std::string line;
    std::getline(istr,line);

    std::stringstream lstr(line);
    std::string cell;

    while(std::getline(lstr,cell, ',')) {
        output.push_back(cell);
    }
    // This checks for a trailing comma with no data after it.
    if (!lstr && cell.empty()) {
        // If there was a trailing comma then add an empty element.
        output.push_back("");
    }
    return output;
}

int main() {
	std::string filename;
  std::cout << "Program started!\nPlease type the name of your .csv file below\n(not including the file extension.)\n";
	std::cin >> filename;

	std::cout << "Attempting to open file " << filename << ".csv\n";

	std::ifstream inStream(filename + ".csv");

	if (inStream.fail()) {
		std::cout << "Could not open file " << filename << ".csv\n";
		std::cout << "Exiting...\n";
		return 1;
	} else {
		std::cout << "Successfully opened file. Reading...\n";
	}

	std::string line;
	int linesProcessed = 0;
	std::vector<std::pair<std::string, int>> tableLines;

	while (inStream) {
		std::vector<std::string> lineStr;
		lineStr = getNextLine(inStream);
		std::string cellStr = "";
		std::string cellInt = "";

		//delete blank element
		if (lineStr.at(lineStr.size() - 1) == "") lineStr.resize(lineStr.size() - 1);

		try {
			cellInt = lineStr.at(3);
			cellStr = lineStr.at(2);
		} catch (std::out_of_range) { break; }

		for (int i = 0; i < cellStr.size(); i++) {
			if (cellStr.at(i) == 34) cellStr.erase(i,1);
		}

		std::stringstream intstream(cellInt);
		int outInt;
		intstream >> outInt;

		tableLines.push_back(std::make_pair(cellStr, outInt));

		linesProcessed++;
		std::cout << "Read " << linesProcessed << " lines from CSV.\n";
	}
	inStream.close();
	std::cout << "Done!\n";

	std::ifstream txtStream(filename + ".txt");
	std::cout << "Reading from " << filename << ".txt\n";

	std::vector<std::string> txtLines;
	std::string txtLine;

	linesProcessed = 0;
	while (std::getline(txtStream, txtLine)){
		while (txtLine.at(0) == 9) txtLine.erase(0,1);
		txtLines.push_back(txtLine);
		linesProcessed++;
		std::cout << "Read " << linesProcessed << " lines from TXT.\n";
	}
	txtStream.close();
	std::cout << "Done!\n";

	std::vector<std::string> outputLines;
	std::vector<std::string> unusedLines;

	std::cout << "Comparing CSV to TXT...\n";

	linesProcessed = 0;

	std::ofstream outStream(filename + "_out.txt");
	std::ofstream unusedStream(filename + "_unused.txt");

	for (int i = 0; i < txtLines.size(); i++) {
		bool isUsed = false;
		for (int j = 0; j < tableLines.size(); j++) {
			if (txtLines.at(i) == tableLines.at(j).first) {
				outputLines.push_back(txtLines.at(i) + ", " + std::to_string(tableLines.at(j).second));
				outStream << txtLines.at(i) + ", " + std::to_string(tableLines.at(j).second) + "\n";
				isUsed = true;
			}
		}
		if (!isUsed) {
			unusedLines.push_back(txtLines.at(i));
			unusedStream << txtLines.at(i) << " (TXT)\n";
			//try { unusedStream << tableLines.at(i).first << "(CSV)\n";} catch (std::out_of_range) {}
		}

		std::string usedString;
		if (isUsed)
			usedString = "used";
		else
			usedString = "not used";

		linesProcessed++;
		std::cout << "Compared " << linesProcessed << " lines. Line " << usedString << ".\n";
	}

	outStream.close();
	unusedStream.close();

	std::cout << "Operation complete!\n";

	return 0;
}
?