Advertisement
Guest User

Untitled

a guest
Jun 1st, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <sstream>
  5. #include <map>
  6. #include <vector>
  7.  
  8.  
  9. void Tokenize(const string& str,
  10. vector<string>& tokens,
  11. const string& delimiters = " ")
  12. {
  13. // Skip delimiters at beginning.
  14. string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  15. // Find first "non-delimiter".
  16. string::size_type pos = str.find_first_of(delimiters, lastPos);
  17.  
  18. while (string::npos != pos || string::npos != lastPos)
  19. {
  20. // Found a token, add it to the vector.
  21. tokens.push_back(str.substr(lastPos, pos - lastPos));
  22. // Skip delimiters. Note the "not_of"
  23. lastPos = str.find_first_not_of(delimiters, pos);
  24. // Find next "non-delimiter"
  25. pos = str.find_first_of(delimiters, lastPos);
  26. }
  27. }
  28.  
  29.  
  30. class csvReader;
  31.  
  32. class csvRow {
  33. public:
  34. csvRow(std::vector<std::string> _data, std::map<std::string, int> *_header) {data = _data;header = _header; empty = false;}
  35. csvRow(bool _empty = false) {empty = _empty;}
  36. ~csvRow() {}
  37.  
  38. // TODO: extend below function to report failure when a col_name not present in headers is used.
  39. std::string operator[](const std::string &_col_name) const {return data[header->operator[](_col_name)];}
  40. std::string operator[](int _col_index) const {return data[_col_index];}
  41. bool isEmpty() {return empty;}
  42. private:
  43. bool empty;
  44. std::map<std::string, int> *header;
  45. std::vector<std::string> data;
  46. friend class csvReader;
  47. };
  48.  
  49. class csvReader {
  50. public:
  51. csvReader(std::string _filename);
  52. ~csvReader() {}
  53. csvRow next();
  54. private:
  55. std::ifstream ifs;
  56. std::map<std::string, int> header;
  57. };
  58.  
  59. csvReader::csvReader(std::string _filename) {
  60. ifs.open(_filename.c_str());
  61. std::vector<std::string> header_row = next().data;
  62. std::vector<std::string>::iterator ii;
  63. int i;
  64. for (ii = header_row.begin(), i = 0;
  65. ii != header_row.end();
  66. ii++, i++) {
  67. header[*ii] = i;
  68. }
  69. }
  70.  
  71. csvRow csvReader::next() {
  72.  
  73. std::vector<std::string> result;
  74. std::string line;
  75.  
  76. std::getline(ifs, line);
  77. std::cout << line << endl;
  78. Tokenize(line,result,",");
  79.  
  80. // 1) Make the function exit gracefully when there are no more rows in the csv.
  81. // (i.e. end of file is reached.)
  82. // 2) Each row is read into 'line' var. Split it by ',' and insert to result vector, so that the following insert would make sense.
  83.  
  84. return csvRow(result, &header);
  85. }
  86.  
  87.  
  88. int main(void) {
  89. csvReader rdr("test.csv");
  90. if(!ifs.eof())
  91. {
  92. csvRow current_row = rdr.next();
  93. }
  94. else
  95. {
  96. ifs.close();
  97. }
  98. while (!current_row.isEmpty()) {
  99. std::cout << "User: " << current_row["user"] << std::endl
  100. << "Pass: " << current_row["password"] << std::endl;
  101. current_row = rdr.next();
  102. }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement