Как я могу читать числа с плавающей запятой из файла .txt. В зависимости от имени в начале каждой строки я хочу прочитать разное количество координат. Поплавки разделены «пробелом».
Пример: треугольник 1,2 -2,4 3,0
Результат должен быть:
float x = 1.2 / float y = -2.4 / float z = 3.0
В файле больше строк с разными формами, которые могут быть более сложными, но я думаю, что если я знаю, как сделать одну из них, я смогу сделать другие самостоятельно.
Мой код на данный момент:
#include <iostream>
#include <fstream>
using namespace std;
int main(void)
{
ifstream source; // build a read-Stream
source.open("text.txt", ios_base::in); // open data
if (!source) { // if it does not work
cerr << "Can't open Data!\n";
}
else { // if it worked
char c;
source.get(c); // get first character
if(c == 't'){ // if c is 't' read in 3 floats
float x;
float y;
float z;
while(c != ' '){ // go to the next space
source.get(c);
}
//TO DO ?????? // but now I don't know how to read the floats
}
else if(c == 'r'){ // only two floats needed
float x;
float y;
while(c != ' '){ // go to the next space
source.get(c);
}
//TO DO ??????
}
else if(c == 'p'){ // only one float needed
float x;
while(c != ' '){ // go to the next space
source.get(c);
}
//TODO ???????
}
else{
cerr << "Unknown shape!\n";
}
}
return 0;
}