In my work a txt file is read, among the data in it, there are geographical positions of the vehicles. Due to a problem I am having at the time of reading this file, I want to read this information as the string type, but I need to store the position information as the Coord type. Is it possible to convert a string to Coord?
Error:
error: invalid operands to binary expression ('__istream_type' (aka 'basic_istream<char, std::char_traits<char> >') and 'veins::Coord')
The error is shown when "while ()" reads the "position" variable as type Coord. The way I thought it would be easier would be to get the position as a string and then convert it to the Coord type and store it in chData.position.
Code of the method used:
std::ifstream file;
file.open("chFile.txt");
if(file.is_open()) {
int sector;
int ch;
Coord position;
while (file >> sector >> ch >> position) {
chTableStruct chData;
chData.ch = ch;
chData.position = position;
chTable.insert(std::pair<int,chTableStruct>(sector,chData));
}
file.close();
}
else {
std::cout << "The file chFile.txt cannot be opened! it exists?" << endl;
}
chFile.txt file:
2 20 (401.467,223.4,0)
3 52 (201.446,223.4,0)
1 31 (201.461,623.4,0)
First of all, sorry my poor English...
When I need to read a string and convert to Coord, in VEINS + OMNeT simulator I do this:
Coord MyClass::strToCoord(std::string coordStr){
//this 2 lines is will remove () if necessary
coordStr.erase(remove(coordStr.begin(), coordStr.end(), '('), coordStr.end()); //remove '(' from string
coordStr.erase(remove(coordStr.begin(), coordStr.end(), ')'), coordStr.end()); //remove ')' from string
char coordCh[30]; //30 or a sufficiently large number
strcpy(coordCh,coordStr.c_str());
double x = 0, y = 0, z = 0;
char *token = strtok(coordCh, ",");
x = stold(token);
token = strtok(NULL, ",");
y = stold(token);
token = strtok(NULL, ",");
z = stold(token);
cout << "x= " << x << ", y= " << y << ", z= " << z << endl; //comment this line to disable debugging
return Coord(x,y,z);
}
But I don't know if there is already a method implemented in VEINS to do this.
I hope I helped you.