Hi people,
is it possible to read a txt file with 3 different delimiters (,;#) into a 3d vector of x,y values ("sf::vector2f")?
The lines of the txt file have varying lengths, so an array is not possible (cpp0x standard).
I've got only an approach that does not work correctly.
The txt file looks like this: (numbers are only exemplaric for better understanding)
1,2,3,4,5,6;7,8,9,10,11,12#
13,14,15,16,17,18#
What I would like to do, is to save the values in the following way:
temptempworldcoordinates3d.push_back(1,2);
temptempworldcoordinates3d.push_back(3,4);
temptempworldcoordinates3d.push_back(5,6);
--> Semikolon Symbol:
tempworldcoordinates3d.push_back(temptempworldcoordinates3d.push_back);
temptempworldcoordinates3d.clear();
temptempworldcoordinates3d.push_back(7,8);
temptempworldcoordinates3d.push_back(9,10);
temptempworldcoordinates3d.push_back(11,12);
--> Hash Symbol:
tempworldcoordinates3d.push_back(temptempworldcoordinates3d.push_back);
worldcoordinates3d.push_back(tempworldcoordinates3d);
temptempworldcoordinates3d.clear();
tempworldcoordinates3d.clear();
temptempworldcoordinates3d.push_back(13,14);
temptempworldcoordinates3d.push_back(15,16);
temptempworldcoordinates3d.push_back(17,18);
--> Hash Symbol:
tempworldcoordinates3d.push_back(temptempworldcoordinates3d.push_back);
worldcoordinates3d.push_back(tempworldcoordinates3d);
temptempworldcoordinates3d.clear();
tempworldcoordinates3d.clear();
How to read it from text file correctly and tokenize it?
Is there a way to do it without external libraries and without too much code.
Any help apperciated!
My approach:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <fstream>
#include <algorithm>
std::vector<std::vector<std::vector<sf::Vector2f>>> worldcoordinates3d;
std::vector<std::vector<sf::Vector2f>> tempworldcoordinates3d;
std::vector<sf::Vector2f> temptempworldcoordinates3d;
int main()
{
sf::RenderWindow window(sf::VideoMode(500,500), "Map", sf::Style::Close);
window.setFramerateLimit(40);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
if(sf::Keyboard::isKeyPressed(sf::Keyboard::L))
{
std::ifstream infile;
infile.open("worldmapcoordinates.txt");
std::string line;
sf::Vector2f vector2fvar = sf::Vector2f(0,0);
std::string str = "";
float floatvarx,floatvary = 0;
while(std::getline(infile, line))
{
std::size_t prev = 0, pos;
while ((pos = line.find_first_of("#,;", prev)) != std::string::npos)
{
if (pos > prev)
{
str = line.substr(prev, pos-prev);
std::istringstream (str) >> floatvarx;
vector2fvar.x = floatvarx;
std::istringstream (str) >> floatvary;
vector2fvar.y = floatvary;
temptempworldcoordinates3d.push_back(vector2fvar);
prev = pos+1;
}
}
if (prev < line.length())
{
str = line.substr(prev, std::string::npos);
std::istringstream (str) >> floatvarx;
vector2fvar.x = floatvarx;
std::istringstream (str) >> floatvary;
vector2fvar.y = floatvary;
temptempworldcoordinates3d.push_back(vector2fvar);
}
}
for(int i=0;i<temptempworldcoordinates3d.size();i++)
{
std::cout << temptempworldcoordinates3d[i].x << " " << temptempworldcoordinates3d[i].y << std::endl;
}
}
}
}
window.clear();
window.display();
}
return 0;
}
|