简体   繁体   English

将文本文件读取为C ++值,并用不同的字符分隔

[英]Reading a text file into C++ values, separated by different characters

I have a text file with some data in it, and I'm trying to read it into my objects. 我有一个文本文件,里面有一些数据,我正在尝试将其读入对象。

It's a fairly simple format consisting of a filename, a pair of dimension values, then a list of value-pairs: 这是一种相当简单的格式,由文件名,一对维度值和一系列值对组成:

StringFileName
IntWidth IntHeight
IntA:IntB IntA:IntB IntA:IntB
IntA:IntB IntA:IntB IntA:IntB
IntA:IntB IntA:IntB IntA:IntB

For example: 例如:

MyFile.txt
3 3
1:2 3:4 4:5
9:2 1:5 2:1
1:5 8:3 4:2
There may be more unrelated text here

Here's what I have so far: 这是我到目前为止的内容:

void OnLoad(char* InputFilename) {
    string Filename;
    int Width;
    int Height;

    // open the file
    ifstream inputFile(InputFilename);

    // get the filename
    getline(inputFile, Filename);
    cout << "Filename is " << Filename << endl;

    // get the width and height
    string dataLine;
    getline(inputFile, dataLine);
    stringstream ss(dataLine);
    ss >> Width;
    ss >> Height;
    cout << "Dimensions are " << Width << "x" << Height << endl;

    // get the lines of tile-data
    for (int Y = 0; Y < Height; Y++) {
        /*
         * Here is where I'm getting stuck.
         * I have a string of "i:i i:i i:i" values, and I get a series of strings 
         * of "i:i" values, but can't work out the neatest way of splitting it out.
         */
        getline(inputFile, dataLine);
        stringstream row(dataLine);
        for (int X = 0; X < Width; X++) {
            int IntA;
            int IntB;
            // ?
            DoStuffWith(IntA, IntB);
        }

   }
}

Something like this should work 这样的事情应该工作

std::ifstream file("filename");
std::string file_name;
int height, width;
file >> file_name >> height >> width;
// i don't know what your format is but it isn't very importance
std::deque<std::pair<int, int> > pairs;
std::string line;
int i, j;
char a;
while(std::cin >> i >> a >> j)  //here i j are values in your pair.
{
     if(a!=':') break;
     pairs.push_back(std::make_pair(i, j));
}

Boost's split function can divide on arbitrarily many predicates. Boost的split函数可以任意划分多个谓词。 So you could do something like: 因此,您可以执行以下操作:

vector<string> results;
boost::split( results, data_string, boost::is_any_of(" :") ); <- splits on spaces and ":"

Then you have all the data in a handy container! 然后,将所有数据保存在一个方便的容器中!

Maybe not the best solution in your case but it's good to keep in mind. 也许这不是您所需要的最佳解决方案,但是请牢记这一点。

Now you can use the "*i" and assign the int values that you get in a more clean way. 现在,您可以使用“ * i”并以更简洁的方式分配您获得的int值。

std::string line;
            do{
                getline(inputFile,line);
                sregex_token_iterator end;

                for(sregex_token_iterator i(line.begin(), line.end(), pattern,-1); i!=end;++i)
                {
                    cout<< *i << " " ;
                }
            }while(!inputFile.eof());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM