0

Well I have that code:

#include <iostream>
#include <string>

using namespace std;

int main()
{
std::string s = "0,0,0,1,1,1,0,0,1";
std::string delimiter = ",";

int x = 0;
std::string mapa[9];

size_t pos = 0;
std::string token;
while ((pos = s.find(delimiter)) != std::string::npos) {
    token = s.substr(0, pos);
    std::cout << token << std::endl;
    s.erase(0, pos + delimiter.length());

    mapa[x] = token;
    x++;
}
std::cout << s << std::endl;
cin.get();
}

Parse (split) a string in C++ using string delimiter (standard C++)

I have x array, but I need a second dimension the Y... I get the content from a text file called map.txt:

0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

And I need to split it by commas (for the x) and later by newlines (for y)...

But Idk how to do the Y array... What Can I do?

Thanks!

Community
  • 1
  • 1
Seazoux
  • 615
  • 1
  • 5
  • 28

4 Answers4

1

You may read the lines from the file as

fstream fstr;
fstr.open("file.txt",ios::in);
string str;
int yDimension = 0;
while(getline(fstr,str)
{
    yDimension++;   //do appropriate thing with the y dimension
    std::string token;
    while ((pos = str.find(delimiter)) != std::string::npos) {
        token = str.substr(0, pos);
        std::cout << token << std::endl;
        str.erase(0, pos + delimiter.length());
        mapa[x] = token;
        x++;
    }
}
Saksham
  • 8,110
  • 6
  • 35
  • 63
1

You can read the entire file of any number of rows with any number of comma-delimited columns (memory-permitting) with this:

#include <algorithm>
#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <sstream>
#include <vector>

struct int_reader : std::ctype<char>
{
    int_reader() : std::ctype<char>(get_table()) {}

    static std::ctype_base::mask const* get_table()
    {
        static std::vector<std::ctype_base::mask> rc(table_size, std::ctype_base::mask());
        rc[','] = std::ctype_base::space;
        rc['\n'] = std::ctype_base::space;
        return &rc[0];
    }
};

int main()
{
    std::vector<std::vector<int> > myFileData;
    std::ifstream fin("MyDataFile.txt", std::ifstream::in);
    std::string buffer;
    while (std::getline(fin, buffer))
    {
        std::stringstream ss(buffer);
        std::vector<int> t;
            int_reader reader;
        ss.imbue(std::locale(std::locale(), &reader));
        std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(), std::back_inserter(t));
        myFileData.push_back(t);
    }
    // do whatever you need to with the loaded arrays ...
    return 0;
}
Zac Howland
  • 15,149
  • 1
  • 23
  • 37
  • But I don't see any cout and any array.. :/ – Seazoux Sep 19 '13 at 18:37
  • I left that to you. After you read it in, you can do whatever you need with it. – Zac Howland Sep 19 '13 at 18:39
  • Wow... xD I'm so noob, I don't see the arrays, let me find it... xD (Thanks for the work... :D) – Seazoux Sep 19 '13 at 18:41
  • Well, to be semantically correct, the "arrays" in this solution are actually `vector`s - dynamic arrays where the memory is managed for you. – Zac Howland Sep 19 '13 at 18:43
  • Ok, well vectors, but how can I call them with a cout? xD – Seazoux Sep 19 '13 at 18:44
  • If you just want to print everything you read in, you can do something like: `for_each(myFileData.begin(), myFileData.end(), [&](const std::vector& v) { std::copy(v.begin(), v.end(), std::ostream_iterator(std::cout, " ")); std::cout << std::endl; });`. But you can access the elements of each `vector` using the subscripts, just like normal arrays (e.g. `myFileData[0][0]` will give you the first `int` in the first array). – Zac Howland Sep 19 '13 at 18:54
  • Wow! Thanks... The only thing is that x and y is inverted! LOL xd – Seazoux Sep 19 '13 at 19:04
0

You can use ifstream::getline(), with a delimiter ','

ifstream file ( "map.txt" ); 
string value; 
while ( file.good() ) 
{
 getline ( file, value, ',' ); 
}

Or you can read all the text and use regular expression to take each text out between delimiter.

jgmao
  • 127
  • 2
  • 8
0

The sortest way to do that without vectors:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{

fstream fstr;
fstr.open("mapa.txt");

char mapa[31][9];
int x = 0, y = 0;
char c;
while(fstr.good())
{
    c = fstr.get();
    if (c!= ',') {
        mapa[x][y] = c;
        x++;
    }
    if (c=='\n')
    {
        x = 0;
        y++;
    }
}

fstr.close();
cin.get();

}

Only 32 lines!! :D

Seazoux
  • 615
  • 1
  • 5
  • 28