简体   繁体   中英

How to read in strings, based upon the first, and then third character, to a 2D string array in C++

So i have a text file of 35 lines, with the following format:

number [space] string [space] number [space] number [newline]

Now I wish to read the content of that text file into an array. But in a way that each line of the file is also represented in a separate array of its own. Thus creating a two-dimensional array.

Pseudo example:

myFileContentInArrayFormat = [
    [1, "string", 155 29],
    [2, "aa", 425 96],
    [3, "blabla", 8375 2099]
]

My attempt is as follows and obviously does not read it in correctly.

    void player::readToArray(ifstream& infile, string filename)
    {
    infile.open(filename);


    for (int i = 0; i < 35; i++)
    {
        for (int e = 0; e < 13; e++)
        {
            infile >> Array[i][e];
        }
    }
}

I think I understand what you mean. You want to have all lines of your file into an array. But also want to be able to access each part - which are separated by a space - of that line by putting it into a vector.

Code:

#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
//You probably want to take a look at vectors instead of arrays
#include <vector>

//readToArray
//:param filename: Name of the file to read from
std::vector< std::vector<std::string> > readToArray(std::string filename) {
    //Declaring ifstream here, can be from parameter too like in your code but why would you do that?
    std::ifstream file(filename);
    std::string line;
    std::vector<std::string> fileContent;

    //Check if file succesfully opened
    if (file.is_open()) {
        //Read all the lines and put them in the vector
        while (getline(file, line)) {
            fileContent.push_back(line);
        }

        file.close();
    } else {
        std::cerr << "Error opening file " << filename << std::endl;
    }

    //Now all the lines are in the vector fileContent in string format
    //Now create your two dimensional array (with the help of vectors)
    std::vector< std::vector<std::string> > twoDimensionalVector;

    //Loop over all lines
    for (std::string& line : fileContent) {
        //Splitting the contents of the file by space, into a vector
        std::vector<std::string> inlineVector;
        std::istringstream iss(line);
        for(std::string s; iss >> s; )
            inlineVector.push_back(s);

        //Now push this separated line (which is now a vector to the final vector)
        twoDimensionalVector.push_back(inlineVector);
    }

    //Return
    return twoDimensionalVector;

}

int main() {
    std::string filename = "test.txt";
    std::vector< std::vector<std::string> > myVector = readToArray(filename);

    //Now print out vector content
    for (std::vector<std::string>& vec : myVector) {
        //Print out vector content of the vector in myVector
        std::cout << "[ ";
        for (std::string& str : vec) {
            std::cout << str << " ";
        }
        std::cout << "]" << std::endl;
    }
}

test.txt

1 aaa 67 777
2 bbb 33 663
3 ccc 56 774
4 ddd 32 882
5 eee 43 995

You can access specific parts of your file by using the indexes of the vector. If you want to for example print out 'ddd' which is on line four the second part. you use:

//Access 'ddd' (line 4 second part)
std::cout << myVector[3][1] << std::endl;

Be aware of zero-indexing of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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