0

好吧,我有那个代码:

#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();
}

使用字符串分隔符(标准 C++)在 C++ 中解析(拆分)字符串

我有 x 数组,但我需要第二维 Y...我从名为 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

而且我需要用逗号(对于x)和后来的换行符(对于y)来分割它......

但是 Idk 如何做 Y 数组......我能做什么?

谢谢!

4

4 回答 4

1

您可以将文件中的行读取为

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++;
    }
}
于 2013-09-19T18:12:14.737 回答
1

您可以使用以下命令读取具有任意数量的逗号分隔列(内存允许)的任意行数的整个文件:

#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;
}
于 2013-09-19T18:32:57.663 回答
0

您可以使用 ifstream::getline(),带有分隔符 ','

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

或者您可以阅读所有文本并使用正则表达式在分隔符之间取出每个文本。

于 2013-09-19T18:19:05.970 回答
0

没有向量的最简单的方法:

#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();

}

只有32行!!:D

于 2013-09-20T17:46:53.927 回答