Home > other >  How can I read a specific value c using a text file to store in data structure
How can I read a specific value c using a text file to store in data structure

Time:04-29

How do i seperate my text file values to store in a vector

I have a file.txt with values such as

DOB,NAME,Arrival,Depature
12/04/2021,Dennis,12:30:20,14:30:40
10/03/2001,Sam,14:20:30,-

I want to store these values in a vector I have figured out how to store all of them in the vector but now i am creating another vector where i want to store all the values without a depature time so in this case Sam '-' which has this value. and another vector which will store Dennis which already has a depature time

What i have done so far

name *v = new name;
    ifstream myFileStream("FILE.txt");
    if(!myFileStream.is_open()){
        cout<<"File Failed to open"<<endl;
    }
    string date,name,arrival,depature;
std::string delimiter = "/";
std::string delimiters = ":";


    string line;
    string b;
    string c;
    string d;
    string e;
    std::getline(myFileStream,line);
    while(getline(myFileStream,line)){
        stringstream ss(line);
        getline(ss,date,',');
        //veh.at(i).dt = v->dt;
        size_t pos = 0;
        std::string token;
        while ((pos = date.find(delimiter)) != std::string::npos) {
            c=token;
            token = date.substr(0, pos);
            b=token;

            date.erase(0, pos   delimiter.length());

        }
        v->dt.year = stoi(date);
        v->dt.month = stoi(b);
        v->dt.day = stoi(c);
        veh.at(i).dt.month = v->dt.month;
        veh.at(i).dt.year = v->dt.year;
        veh.at(i).dt.day = v->dt.day;
        getline(ss,v->name,',');
        veh.at(i).pltno = v->name;
        getline(ss,arrival,',');
         size_t pose = 0;
        std::string tokens;
        while ((pose = arrival.find(delimiters)) != std::string::npos) {
            e=tokens;
            tokens = arrival.substr(0, pose);
            std::cout << tokens << std::endl;
            d=tokens;

            arrival.erase(0, pose   delimiters.length());
        }
         v->arrive.hh = stoi(e);
        veh.at(i).arrive.hh = v->arrive.hh;
        v->arrive.mm = stoi(d);
        veh.at(i).arrive.mm = v->arrive.mm;
        ((stoi(arrival) <= 9) ? 0 : "");
        v->arrive.ss = stoi(arrival);
        veh.at(i).arrive.ss = v->arrive.ss;
        //veh.at(i).arrive = arrival;
        getline(ss,depature,',');
        //cout<<veh.at(i).arrive<<endl;
        i  ;
    }
    myFileStream.close();

Please guys i really need help with this I am stuck at the moment and really would appreciate someone helping me with the code so far the code above stores the Name arrival DOB into the vector but I am going to create a new vector for people who left and people who have not how can i seperate values which have '-' & which have time and store it in a similar format?

Thank YOU

CodePudding user response:

#include<vector>
#include<string>
#include<fstream>
using namespace std;
void split(const string& s, vector<string>& tokens, char delim = ' ') {
    tokens.clear();
    auto string_find_first_not = [s, delim](size_t pos = 0) -> size_t {
        for (size_t i = pos; i < s.size(); i  ) {
            if (s[i] != delim) return i;
        }
        return string::npos;
    };   
    size_t lastPos = string_find_first_not(0);
    size_t pos = s.find(delim, lastPos);
    while (lastPos != string::npos) {
        tokens.emplace_back(s.substr(lastPos, pos - lastPos));
        lastPos = string_find_first_not(pos);
        pos = s.find(delim, lastPos);
    }
}

struct DOB
{
  string year;
  string month;
  string day;
//  DOB(const string &y,const string &m,const string &d):year(y),month(m),day(d){}
  DOB(const string &dob)
  {
    vector<string> strs;
    split(dob,strs,'/');
    year = strs[2];
    month = strs[0];
    day = strs[1];
  }
};

struct Time
{
  bool left;
  string hour;
  string minute;
  string second;
//  Time(const string &h,const string &m,const string &s):hour(h),minute(m),second(s){}
  Time(const string &time)
  {
    vector<string> strs;
    split(time,strs,':');
    hour = strs[0];
    minute = strs[1];
    second = strs[2];
    left = true;
  }
  Time()
  {
    left = false;
  }
};

struct UserInfo
{
  DOB dob;
  string name;
  Time arrival;
  Time depature;
  UserInfo(const DOB &d,const string &n,const Time &a,const Time &de):dob(d),name(n),arrival(a),depature(de){}
};

int main()
{
  // read and open file
  ifstream file;
    file.open("./file.txt", ios::in);
    if (!file.is_open())
    {
    // error
        return 1;
    }
  vector<UserInfo> leftUsers;
  vector<UserInfo> notleftUsers;

  string buf;
  getline(file,buf);
  // handle file
    while (getline(file,buf))
    {
        if(buf.find("-") == string::npos)
    {
      // left
      vector<string> strs;
      split(buf,strs,',');
      // DOB
      DOB dob(strs[0]);
      // Arrival
      Time arrival(strs[2]);
      Time depature(strs[3]);

      leftUsers.push_back(UserInfo(dob,strs[1],arrival,depature));
    }
    else
    {
      // not left
      vector<string> strs;
      split(buf,strs,',');
      // DOB
      DOB dob(strs[0]);
      // Arrival
      Time arrival(strs[2]);
      Time depature;

      notleftUsers.push_back(UserInfo(dob,strs[1],arrival,depature));
    }
    }
  return 0;
}
  • Related