Reading file with multiple different delimiters

Hello everyone! I have a slight problem:

I have a file called "testfile.txt" and inside is a line:
TR01[100,300,1] TR01[130,310,1] TR01[300,300,1]

Above is an example of a TREES placed at the xy coordinates and lastly the collision flag, which is TRUE in all of the cases. [x,y,collision]
I'm making a game and this is how objects are placed on the map.

---------

It was fairly easy to read a 2D array from a file (for tilemap), but this is making me insane.

I want to store all the objects into a vector.
It should also continue reading the file to find all the objects (When there comes a whitespace, it should know there's the next object).

My mind is blank atm and i have no clue how to do that.

Heres my test code so far:

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
#include <iostream>
#include <vector>
#include <sstream>
#include <string>
#include <fstream>

using namespace std;

int main(int argc, char **argv){
	
	int state = 0;
	enum { Object,x,y,Collision };

	string fileInfo;
	string helper;

	ifstream file("testfile.txt"); 
	if(!file.is_open())
		cout << "Could not load file \n\n";

		while(!file.eof())
		{
			getline(file, fileInfo);
			
			if(fileInfo.find("[") != string::npos) 
			{
			        state = Object;
			        continue;
			}
						
		} //!file.eof

		switch(state)
                {

		case Object:
		{
			stringstream fileStr(fileInfo);	

			while(!fileStr.eof())
			{	
			// ------------- //
			// What to do? - //
			// ------------- //
			}
		break;
		}

		}//switch

	cout << "\n\n";
	system("PAUSE");
	file.close();
	
return 0;
}



Help needed, please :)!
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
#include <string>
#include <fstream>
#include <vector>
#include <iostream>
#include <cctype>

struct info
{
    std::string object_id ;
    int x, y ;
    bool collision ;
};

std::ifstream & operator>>(std::ifstream& in, info & info ) ;
void print( std::ostream& out, const std::vector<info>& v) ;

int main()
{
    std::ifstream in("testfile.txt") ;

    std::vector<info> infos ;

    info input ;
    while ( in >> input )
        infos.push_back(input) ;

    print(std::cout, infos) ;
}

void print_info( std::ostream & out, const info & i )
{
    out << i.object_id << " @ (" << i.x << ',' << i.y << ") collision: " 
        << (i.collision ? "true\n" : "false\n") ;
}


void print( std::ostream& out, const std::vector<info>& v)
{
    for ( auto i = v.begin() ;  i != v.end();  ++i)
        print_info(out, *i) ;
}

std::ifstream & operator>>(std::ifstream& in, info & info )
{
    while ( in && std::isspace(in.peek()) )
        in.ignore() ;

    std::getline( in, info.object_id, '[' ) ;

    in >> info.x ;
    in.ignore() ;  // ,

    in >> info.y;
    in.ignore() ; // ,

    int c ;
    in >> c ;

    info.collision = c != 0 ;

    in.ignore() ; // ]

    return in ;
}

Thank you so much cire! Just what i was looking for!
You are officially awesome to me :)
Topic archived. No new replies allowed.