Hello,
I have to parsing (and to do something) with lines read from a file like 10,50,30,4 (10,22,3,^,22 -> the ^ means the line contains an error so I have to reject it)...
My code seems ok and it's this:
const char delim=',';
void myFunction(vector<string>& lineOfFile, const char& delim) {
vector<string>::iterator it = lineOfFile.begin();
for( ;it != lineOfFile.end(); ++it) {
vector<double> line;
size_t pStart=-1;
size_t pEnd=-2;
double val;
string subStr("");
bool lineIsOk = true;
while (pEnd != -1 && lineIsOk) {
pStart = (int) (*it).find(delim, pStart+1);
pEnd = (int) (*it).find(delim, pStart+1);
subStr = (*it).substr(pStart+1, pEnd-1 - pStart );
if (subStr != "^" ) {
val = (double) atof( subStr.c_str() );
subStr="";
line.push_back(val);
}
else { lineIsOk = false; }
}
if ( lineIsOk) insertInOtherStruvture(line);
}
const char delim=',';
void myFunction(vector<string>& lineOfFile, const char& delim) {
vector<string>::iterator it = lineOfFile.begin();
for( ;it != lineOfFile.end(); ++it) {
vector<double> line;
size_t pStart=-1;
size_t pEnd=-2;
double val;
string subStr("");
bool lineIsOk = true;
while (pEnd != -1 && lineIsOk) {
pStart = (int) (*it).find(delim, pStart+1);
pEnd = (int) (*it).find(delim, pStart+1);
subStr = (*it).substr(pStart+1, pEnd-1 - pStart );
if (subStr != "^" ) {
val = (double) atof( subStr.c_str() );
subStr="";
line.push_back(val);
}
else { lineIsOk = false; }
}
if ( lineIsOk) insertInOtherStruvture(line);
}
To copy to clipboard, switch view to plain text mode
Problem now is that this is working only delim=',' and I'd like parsing that line in way like mine (it seems simple, doens't it?) but with other delim for example, I'd like cover line like
10 2 22 200 1
or
10 space space space 22 space 2 33
if the creator of file don't create the file correct with more space (but I'd like to parse it how it was)
Could you help me, please? thanks a lot.
Bookmarks