Pathfinding from a text file

Pages: 12
closed account (48T7M4Gy)
Just to round it off I tested a few things and I've convinced myself that you have to decide space or comma i.e. whether you won't have the dummy or will have the dummy respectively. I like spaces and you like commas (with the dummy char) - such is the rich tapestry of life. :)
About your comma problem, one thing that other languages have that c++ lacks is a split function. If your data is relatively small (which it looks like it is) you can pull all of it in, then split the data to get your points using a simple split function

1
2
3
4
5
6
7
8
9
10
11
12
13
std::vector<std::string> split(const std::string& instring, const std::string& separator){
    std::vector<std::string> ret;
    long pos = instring.find(separator),prevstart = 0;
    while(pos!=std::string::npos){
        ret.push_back(instring.substr(prevstart,pos-prevstart));
        prevstart=pos+separator.size();
        pos = instring.find(separator, pos+separator.size());
    }
    if (prevstart <= instring.size()){
        ret.push_back(instring.substr(prevstart,instring.size()-prevstart));
    }
    return ret;
}


Usage:

1
2
3
4
5
6
7
8
std::vector<point> points;
std::vector<std::string> lines = split(data,"\r\n");
for(unsigned int a = 0; a < lines.size(); a++){
    std::vector<std::string> components = split(lines[a],",");
    if (components.size()>=2){
        points.push_back(point(components[0],components[1]));
    }
}


The point structure is not defined in this code. And the data member would be the raw data from your file if the data is in a format of

x,y
x,y
x,y

where the newlines are windows carriage return/new lines.
Topic archived. No new replies allowed.
Pages: 12