1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
typedef std::vector< std::string > StringList;
typedef std::list< StringList > MultiStringList;
StringList splitLine (const std::string &buffer, const std::string &separator )
{
StringList result;
std::string textToProcess (pText);
std::string::size_type pos (textToProcess.find (pSeparator));
while (pos != textToProcess.npos) {
result.push_back (textToProcess.substr (0, pos));
textToProcess = textToProcess.substr (pos + pSeparator.size ());
pos = textToProcess.find (pSeparator);
}
if (! textToProcess.empty()) {
result.push_back (textToProcess);
}
return result;
}
int main (int argc, char **argv) {
std::ifstream inputFile ("fichier.csv");
if (inputFile) {
std::string buffer;
MultiStringList listString;
while (std::getline (inputFile, buffer)) {
listString.push_back (splitLine (buffer, ";"));
}
MultiStringList::const_iterator itFirst = listString.begin ();
int LineCount = 0;
for ( ; itFirst != listString.end (); ++itFirst) {
std::cout << "Ligne : " << (++LineCount) << std::endl;
StringList::const_iterator itSecond = itFirst.begin ();
int FieldCount = 0;
for ( ; itSecond != itFirst.end (); ++itSecond) {
std::cout << "Champ[" << (++FieldCount) << "] : " << *itSecond << std::endl;
}
}
}
} |
Partager