Converting coordinates in text form to an array (or best way to process)

The .text file reads something as such:

[1,10]-9-Lunamaria
[7,12]-3-Decathalon
[2,11]-3-Decathalon
[13,9]-8-Magnaria

Essentially, I want to learn to plot these coordinates on an array, corresponding to the [x,y] coordinates and showing the following digit as an identifier, while also storing the name of the identifier.

I was thinking of using getline, but am not too sure how it responds to this.

Reading a whole line into memory and then parsing it is a good way to go.
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
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

// There are many ways to parse a line :)
void parseLine(string line) {
    istringstream is(line);
    is.ignore(1,'[');
    int x;
    is >> x;
    cout << "Found x=" << x << endl;
}

void parseFile(string filename) {
    ifstream in(filename);
    string line;
    while ( getline(in,line) ) {
        parseLine(line);
    }
}

int main() {
    //parseFile("file.txt");
    // Testing parseLine first
    parseLine("[1,10]-9-Lunamaria");
    return 0;
}


$ g++ -std=c++11 main.cpp
$ ./a.out 
Found x=1
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
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;


struct ITEM
{
   int x, y;
   int identifier;
   string name;
};


istream & operator >> ( istream &strm, ITEM &item )
{
   string line;
   getline( strm, line );

   const string junk = "[,]-";
   replace_if( line.begin(), line.end(), [ junk ]( char c ){ return junk.find( c ) != string::npos; }, ' ' );

   stringstream ss( line );
   ss >> item.x >> item.y >> item.identifier >> ws;
   getline( ss, item.name );   // might be multiple words

   return strm;
}


void readData( istream &strm, vector<ITEM> &result )
{
   for ( ITEM item; strm >> item; ) result.push_back( item );
}


void plotData( const vector<ITEM> data )
{
   const int XMAX = 14, YMAX = 14;
   string plot[YMAX];   for ( string &p : plot ) p = string( XMAX, ' ' );
   for ( ITEM item : data ) plot[item.y][item.x] = (char)( '0' + item.identifier );

   cout << string( XMAX, '-' ) << '\n';
   for ( int y = YMAX - 1; y >= 0; y-- ) cout << plot[y] << '\n';
   cout << string( XMAX, '-' ) << '\n';

   cout << "Key:\n";
   for ( ITEM item : data ) cout << item.identifier << ": " << item.name << "   (position: " << item.x << " " << item.y << ")\n";
}


int main()
{
// ifstream in( "data.txt" );
   stringstream in( "[1,10]-9-Lunamaria\n"
                    "[7,12]-3-Decathalon\n"
                    "[2,11]-4-Lupus Magna\n"
                    "[13,9]-8-Magnaria\n" );
   vector<ITEM> data;
   readData( in, data );
   plotData( data );
}



--------------
              
       3      
  4           
 9            
             8
              
              
              
              
              
              
              
              
              
--------------
Key:
9: Lunamaria   (position: 1 10)
3: Decathalon   (position: 7 12)
4: Lupus Magna   (position: 2 11)
8: Magnaria   (position: 13 9)

Last edited on
Topic archived. No new replies allowed.