Home > Mobile >  Read from comma separated file into vector of objects
Read from comma separated file into vector of objects

Time:12-28

I have a done a simple C program to gain knowledge in C . It's a game which stores and reads in the end to a file. Score, Name etc.. At each line in the file the content for a Player object is stored.

Ex: ID Age Name etc.

I now wanted to change to comma separation in the file but then I faced the issue how to read each line and write the Player object into a vector of Player objects std::vector correct.

My code today is like.

std::vector<Player> readPlayerToVector()
{
    // Open the File
    std::ifstream in("players.txt");

    std::vector<Player> players; // Empty player vector

    while (in.good()) {
        Player temp; //
        in >> temp.pID;
        ....
        players.push_back(temp);
    }
    in.close();
    return players;
}

How should I change this code to be compatible with comma separation. Not it works with space separation with the overload of >>.

Be aware that I am a beginner in C . I've tried looking of the examples where std::getline(ss, line) with stringstream is used but I can't figure out a good way to assign the Player object with that method.

CodePudding user response:

I provided a similar solution here:

read .dat file in c and create to multiple data types

#include <iostream>
#include <sstream>
#include <vector>


struct Coefficients {
    unsigned A;
    std::vector<double> B;
    std::vector< std::vector<double> > C;
};

std::vector<double> parseFloats( const std::string& s ) {
    std::istringstream isf( s );
    std::vector<double> res;
    while ( isf.good() ) {
        double value;
        isf >> value;
        res.push_back( value );
    }
    return res;
}

void readCoefficients( std::istream& fs, Coefficients& c ) {
    fs >> c.A;
    std::ws( fs );
    std::string line;
    std::getline( fs, line );
    c.B = parseFloats( line );
    while ( std::getline( fs, line ) ) {
        c.C.push_back( parseFloats( line ) );
    }
}

This one also might apply:

Best way to read a files contents and separate different data types into separate vectors in C

    std::vector<int> integers;
    std::vector<std::string> strings;

    // open file and iterate
    std::ifstream file( "filepath.txt" );
    while ( file ) {

        // read one line
        std::string line;
        std::getline(file, line, '\n');

        // create stream for fields
        std::istringstream ils( line );
        std::string token;

        // read integer (I like to parse it and convert separated)
        if ( !std::getline(ils, token, ',') ) continue;
        int ivalue;
        try { 
            ivalue = std::stoi( token );
        } catch (...) {
            continue;
        }
        integers.push_back(  ivalue );

        // Read string
        if ( !std::getline( ils, token, ',' )) continue;
        strings.push_back( token );
    }

CodePudding user response:

You could separate each variable by line rather than comma. I find this approach much more simple as you can use the getline function.

Have a read of the documentation of ifstream/ofstream. I've done several projects based of this documentation alone!

C fstream reference

  •  Tags:  
  • c
  • Related