This program demonstrates how you can read the tile definition into a 2D vector. It's really a vector of a vector of pairs of numbers. We can improve on this for the final version, but this will demonstrate file handling, streams and how you can break up complex type definitions with typedef.
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
class Tile
{
struct CoordType
{
int x, y;
};
typedef std::vector<CoordType> LineType;
typedef std::vector<LineType> GridType;
GridType grid;
public:
void LoadFromFile(std::string filename);
void SaveToFile(std::string filename);
};
void Tile::LoadFromFile(std::string filename)
{
std::ifstream infile(filename.c_str()); // open the file as an input stream
std::string line;
while (std::getline(infile, line)) // read the next line from the stream into a string
{
LineType row;
std::istringstream ss(line); // create an input stream from the string we read from the file
CoordType value;
char c;
while (ss >> value.x >> c >> value.y) // read the next set of coordinates from the file
{
row.push_back(value); // push the value pair onto a row
}
grid.push_back(row); // push the row onto the grid
}
}
void Tile::SaveToFile(std::string filename)
{
std::ofstream outfile(filename.c_str()); // create the output file as a stream
for (size_t row = 0; row < grid.size(); ++row)
{
for (size_t col = 0; col < grid[row].size(); ++col)
{
if (col != 0)
outfile << " ";
outfile << grid[row][col].x << ":" << grid[row][col].y;
}
outfile << std::endl;
}
}
int main()
{
Tile t;
t.LoadFromFile("C:/wrconv 0.01/bin/Debug/maps/map.txt");
t.SaveToFile("C:/wrconv 0.01/bin/Debug/maps/savedmap.txt");
return 0;
}
ok i will need sometimes to study what you posted and tested but i will reply i do not know how to thank you. give me two days on wendsday i will replay thanks again
yes i will mark as solved the problem thanks a lot i would like just to ask some more question on what you post didn't change anything because it works.
on this part :
while (std::getline(infile, line)) //loop between the line of the file
{
LineType row;
std::istringstream ss(line); // it is is similar to a substring of line? what ss stand for
CoordType value;
char c;// how can the program read c?
while (ss >> value.x >> c >> value.y) // didn't understand how can possible the program stop after every pair?
{
row.push_back(value);
}
grid.push_back(row);
}
thanks again
Your data is row/column data. When you read the data, you need to process a line/row at a time.
In modern languages, we try to read from/write to steams. These streams can be bound to physical files, logical files, memory blocks, ...
Back to a line in your file. We read each line with getlne. Then we use a istringstream to create an input stream from the value we read from the file. Then we use standard istream methods (>>) to read read the fields in that stream. I have commented the code.
sorry to bother you but this is what i do not get neither google it
so istringstream take the all line?
exemple: 1:0 1:0 1:0 1:0 1:0 1:0 1:0 1:0 1:0
ok so how can i define the field?
so in your prog SS it is 1 and C 0 nad so on
if make the file 1;0 1;0 etc....
would be the same?
what it is the the difference between a substring and istringstream?
yes ok that is what i was looking for so basically whit ss you set a field probably instead of ss i can use any word do not why but many use iss.
then you declare a character c and make the computer lokking till there is no character so probably ss could store 1:0 and c 0 in case we have a file 1:0:0 1:1:1.
so to make it feel the computer the space separation i have just to look for a caracter that's genuis.
i understand roughly what it is a string and stream
with string you can take only a certain set of character in a file you can specify also a buffer
i use size t sometime to cut substring (pos or found).
never used stream before i think it is something bigger still studyiing thanks a lot
just to let you know my program it is up and running one of this days you will see on internet much appreciate our discussion thanks