Skip to content

Instantly share code, notes, and snippets.

@devendranaga
Created December 12, 2018 05:14
Show Gist options
  • Save devendranaga/a72ea56d9d2f491e1a31b20a5cdeaeb7 to your computer and use it in GitHub Desktop.
Save devendranaga/a72ea56d9d2f491e1a31b20a5cdeaeb7 to your computer and use it in GitHub Desktop.
simple CSV parser Class
#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