Joel Seah
Joel Seah

Reputation: 714

Pull data from text file into a struct

I'm currently having problem trying to pull data from a textfile using a struct and then storing it into a vector. But no matter wad I do, unless i change the values of float,int to strings, it will always give me errors like this:

MissionPlan.cpp:190: error: invalid conversion from ‘void*’ to ‘char**’
MissionPlan.cpp:190: error: cannot convert ‘float’ to ‘size_t*’ for argument ‘2’ to ‘__ssize_t getline(char**, size_t*, FILE*)

This is my struct:

struct CivIndexDB {
float civInd;
int x;
int y;
}

This is my example text file:

3.2341:2:3
1.5234:3:4

This is the code that I use to extract the data from the textfile and then storing it into a vector:

string line = "";
while (getline(civIndexFile,line)) {
    stringstream linestream(line);

    getline(linestream,civDb.civInd,':');
    getline(linestream,civDb.x,':');
    getline(linestream,civDb.y);
    civIndexesList.push_back(civDb);            
}

Changing the variable types in the struct to string is not what I need as later in the application, I need to sort the vector values based on its float value.

I appreciate any help given. Thanks!

Upvotes: 1

Views: 1038

Answers (2)

jrd1
jrd1

Reputation: 10726

How about this?

#include <vector>
#include <cstdlib>
#include <sstream>
#include <string>
#include <fstream>
#include <iostream>

struct CivIndexDB {
    float civInd;
    int x;
    int y;
};

int main() {
    std::ifstream civIndexFile;
    std::string filename = "data.dat";

    civIndexFile.open(filename.c_str(), std::ios::in);

    std::vector<CivIndexDB> civ;
    CivIndexDB cid;

    if (civIndexFile.is_open()) {
        std::string temp;

        while(std::getline(civIndexFile, temp)) {
            std::istringstream iss(temp);
            int param = 0;
            int x=0, y=0;
            float id = 0;

            while(std::getline(iss, temp, ':')) {
                std::istringstream ist(temp);
                if (param == 0) {
                    (ist >> id) ? cid.civInd = id : cid.civInd = 0;
                }
                else if (param == 1) {
                    (ist >> x) ? cid.x = x : cid.x = 0;
                }
                else if (param == 2) {
                    (ist >> y) ? cid.y = y : cid.y = 0;
                }
                ++param;
            }
            civ.push_back(cid);
        }
    }   
    else {
        std::cerr << "There was a problem opening the file!\n";
        exit(1);
    }

    for (int i = 0; i < civ.size(); ++i) {
        cid = civ[i];
        std::cout << cid.civInd << " " << cid.x << " " << cid.y << std::endl;
    }

    return 0;
}

Upvotes: 0

P0W
P0W

Reputation: 47854

Without looking into your exact problem/errors, I suggest, if the file format is fixed simplest way would be :

char ch; // For ':'
while (civIndexFile >> civDb.civInd >> ch >> civDb.x >> ch >> civDb.y )
{
    civIndexesList.push_back(civDb); 
}

Edit

For sorting on float values you can overload < operator :

struct CivIndexDB {
  float civInd;
  int x;
  int y;

  bool operator <(const CivIndexDB& db) const
  {
    return db.civInd > civInd;
  }
};

And then use std::sort :

std::sort(civIndexesList.begin(), civIndexesList.end() );

Upvotes: 3

Related Questions