Created
December 12, 2018 05:14
-
-
Save devendranaga/a72ea56d9d2f491e1a31b20a5cdeaeb7 to your computer and use it in GitHub Desktop.
simple CSV parser Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <cstring> | |
#include <vector> | |
class csvParse { | |
public: | |
csvParse(std::string input, std::vector<std::string> &output); | |
}; | |
csvParse::csvParse(std::string input, std::vector<std::string> &output) | |
{ | |
int i; | |
int j; | |
j = 0; | |
char s[1024]; | |
for (i = 0; i != input.length(); i ++) { | |
if ((input[i] != '\0') && (input[i] != ',')) { | |
s[j] = input[i]; | |
j ++; | |
} else { | |
s[j] = '\0'; | |
j = 0; | |
output.push_back(std::string(s)); | |
memset(s, 0, sizeof(s)); | |
} | |
} | |
s[j] = '\0'; | |
output.push_back(std::string(s)); | |
} | |
#if 0 | |
int main() | |
{ | |
std::string input {"1,2,3,4,5,6"}; | |
std::vector<std::string> list; | |
csvParse c(input, list); | |
std::vector<std::string>::const_iterator it; | |
for (it = list.begin(); it != list.end(); it ++) { | |
std::cout << *it << std::endl; | |
} | |
list.empty(); | |
return 0; | |
} | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment