Skip to content

Instantly share code, notes, and snippets.

@GreenLightning
Last active April 7, 2016 19:59
Show Gist options
  • Save GreenLightning/b6e3e4cab076fba4dee708d2dddbe2a7 to your computer and use it in GitHub Desktop.
Save GreenLightning/b6e3e4cab076fba4dee708d2dddbe2a7 to your computer and use it in GitHub Desktop.
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
vector<string> split(const string& line) {
vector<string> result;
string::size_type i = 0;
while (i <= line.length()) {
string::size_type j = line.find(';', i);
result.push_back(line.substr(i, j - i));
i = (j == string::npos) ? string::npos : j + 1;
}
return result;
}
int main(int argc, char* argv[]) {
if (argc != 2) {
cout << "usage: " << argv[0] << " <filename>" << endl;
return -1;
}
string filename = argv[1];
ifstream file(filename);
if (!file) {
cout << "could not open " << filename << endl;
return -2;
}
vector<string> lines;
string tmp;
while (getline(file, tmp))
lines.push_back(tmp);
for (const string& line : lines) {
int sum = 0;
for (const string& value : split(line))
sum += stoi(value);
cout << sum << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment