Skip to content

Instantly share code, notes, and snippets.

@Azoay
Last active August 29, 2015 14:25
Show Gist options
  • Save Azoay/77a50bfb15032abca802 to your computer and use it in GitHub Desktop.
Save Azoay/77a50bfb15032abca802 to your computer and use it in GitHub Desktop.
read tsv(c++)
1 39
2 43
3 39
4 32
5 32
6 39
7 33
8 32
9 25
10 32
[0][0]: 1
[0][1]: 39
[1][0]: 2
[1][1]: 43
[2][0]: 3
[2][1]: 39
[3][0]: 4
[3][1]: 32
[4][0]: 5
[4][1]: 32
[5][0]: 6
[5][1]: 39
[6][0]: 7
[6][1]: 33
[7][0]: 8
[7][1]: 32
[8][0]: 9
[8][1]: 25
[9][0]: 10
[9][1]: 32
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <cstring>
#include <string>
#include <ctime>
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <stack>
#include <queue>
#include <bitset>
#include <algorithm>
#include <numeric>
#include <utility>
#include <sstream>
#define ABS(a) ((a) < 0 ? - (a) : (a))
#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)
std::vector<std::vector<std::string> > items;
void read_tsv(char* fname) {
std::ifstream ifs(fname);
if (ifs.fail()) {
std::cerr << "error" << std::endl;
return;
}
std::string line;
while (getline(ifs, line)) {
std::stringstream ss(line);
std::vector<std::string> item;
std::string tmp;
while(getline(ss, tmp, '\t')) {
item.push_back(tmp);
}
items.push_back(item);
}
REP(i,items.size()) {
REP(j, items[i].size())
std::cout << "[" << i << "][" << j <<"]: " << items[i][j] << std::endl;
}
return;
}
int main(int argc, char* argv[]) {
if (argc < 2) {
std::cerr << "usage: " << argv[0] << " file" << std::endl;
std::exit(-1);
}
read_tsv(argv[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment