Hi

Pages: 12
Lets start with reading:
1:0 1:0 1:0 1:0 1:0 1:0 1:0 1:0 1:0
1:0 1:0 1:0 1:0 2:0 1:0 1:0 1:0 1:0
1:0 1:0 1:0 1:0 1:0 3:0 1:0 1:0 1:0

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.

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
#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

i would like just to ask some more question
What's the question?
the one above written as comment mainly if can explain better maybe trying to paraphrase in simple word istringstream and c what it is SS
thanks
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?
Why don't you run the program in a debugger or use printf to look at the value of variables to see what the program is doing?

I use the istringstream to parse the line. The line:
 
while (ss >> value.x  >> c >> value.y)
reads each set of values like 1:0 and breaks out of the look when there's nothing further to read in the stream.

value.x reads the 1, c reads the :, value.y reads the 0 for 1:0.

You could use the string operations to parse the line, but using a stream is a much cleaner solution.

Do you understand what a stream is? Maybe that's the problem.
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
Topic archived. No new replies allowed.
Pages: 12