No dobra, teraz jest kolejny problem. Bardzo podobny ale odwrotny.
Nie wczytuję danych w taki sposób data >> num
a za pomocą getline(istream& is, string& str);
#include <iostream>
#include <fstream>
#include <array>
#include <vector>
#include <string>
#include <sstream>
struct rectangle
{
std::size_t id;
std::size_t posX, posY;
std::size_t width, height;
};
std::string prepareData(std::string& str)
{
//effect of this function:
//#2 @ 153,186: 20x26
//prepareData()
// 2 153 186 20 26
//id, posX, posY, width, height
for (auto itr = str.begin(); itr != str.end(); itr++)
{
if (*itr == '#')
*itr = ' ';
else if (*itr == ',')
*itr = ' ';
else if (*itr == '@')
*itr = ' ';
else if (*itr == ':')
*itr = ' ';
else if (*itr == 'x')
*itr = ' ';
}
return str;
}
int main()
{
std::size_t x, y; //area size
std::vector<rectangle> Rects;
std::ifstream file("input.txt");
if (file.is_open())
{
rectangle rect;
std::string buf;
int num;
while (!file.eof())
{
//get line of rect info
getline(file, buf);
//prepare data
buf = prepareData(buf);
// loading rect info to string stream
std::stringstream sstream(buf);
// getting id
sstream >> buf;
rect.id = std::stoi(buf);
//getting posX and posY
sstream >> buf;
rect.posX = std::stoi(buf);
sstream >> buf;
rect.posY = std::stoi(buf);
//getting width and height
sstream >> buf;
rect.width = std::stoi(buf);
sstream >> buf;
rect.height = std::stoi(buf);
//Adding rectangle to data base
Rects.push_back(rect);
if (rect.id == 1399)
break;
}
file.close();
}
else { std::cerr << "Error opening file!" << std::endl; }
//std::array<std::array<std::size_t, x>, y>;
for (auto &p : Rects)
std::cout << "ID#" << p.id << " pos: " << p.posX << "," << p.posY << " dims: " << p.width << "x" << p.height << std::endl;
std::cin.get();
}
No i nie wiem jak sprawić, żeby pętla zatrzymała się w odpowiednim momencie. Getlinie nie zwraca bool :/
Zjechałem na sam koniec pliku i sprawdziłem końcową wartość, rzuciłem taki warunek i działa ale no ten...
if (rect.id == 1399)
break;
I czy takie podejście odczytywania danych jest poprawne ? Zmieniam dane tak, żeby były wygodne dla mnie.