storing input from text file into 2d array using vectors - c++

So, I need to store the data from the text file into 2d array. I tried using vectors. So here is the sample data from the text file:
START 13
PID 11
CORE 120
SSD 0
CORE 60
SSD 0
CORE 20
SSD 0
I want to store this data as final_vec[x][y]. This is what I tried:
void read_file(const string &fname) {
ifstream in_file(fname);
string line;
vector<string> temp_vec;
vector<vector<string>> final_vec;
while ( getline (in_file,line) )
{
stringstream ss(line);
string value;
while(ss >> value)
{
temp_vec.push_back(value);
}
final_vec.push_back(temp_vec);
}
for (int i = 0; i < final_vec.size(); i++) {
for (int j = 0; j < final_vec[i].size(); j++)
cout << final_vec[i][j] << " ";
cout << endl;
}
}
int main()
{
read_file("test.txt");
return 0;
}
I get error:
main.cpp: In function ‘void read_file(const string&)’:
main.cpp:29:29: error: variable ‘std::stringstream ss’ has initializer but incomplete type
stringstream ss(line);
I am not sure if I am on the right track.

IMHO, a better solution is to model each line as a record, with a struct or class:
struct Record
{
std::string label;
int number;
friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream& operator>>(std::istream& input, Record& r)
{
input >> r.label;
input >> r.number;
return input;
}
The overloaded operator>> makes the input loop a lot simpler:
std::vector<Record> database;
Record r;
while (infile >> r)
{
database.push_back(r);
}
Rather than have a 2d vector of two different types, the above code uses a 1D vector of structures.

Related

How to use getline() with multiple variables in the same line?

I'm trying to read each line from a file and store the data in each line. Say the line is "x y z". What arguments should the getline function use in order to read and store x, y and z individually?
void readData(Gene *data, int num)
{
int codeNum;
int i = 0;
int k = num;
ifstream inputFile;
inputFile.open("example.data");
inputFile >> codeNum;
while(i < k){
getline(inputFile, data[i].geneCode, data[i].MutCode[0],
data[i].MutCost[0], data[i].MutCode[1],
data[i].MutCost[1]);
i++;
}
This is what I have. Note that all the vars I'm trying to read are strings, and that k is the total number of lines. when trying to compile I get an error saying "no matching function to call to getline()" and something about "candidate function template not viable". Any idea what I'm doing wrong?
I highly recommend you use a vector of structures (or classes) rather than multiple, parallel arrays.
struct Mutation_Code_Cost
{
Mutation_Code_Type MutCode;
Mutation_Cost_Type MutCost;
};
struct Gene
{
Gene_Code_Type geneCode;
Mutation_Code_Cost mutation_info[2];
};
You can then overload operator>> to read in the structures from a text stream:
struct Mutation_Code_Cost
{
Mutation_Code_Type MutCode;
Mutation_Cost_Type MutCost;
friend std::istream& operator>>(std::istream& input, Mutation_Code_Cost& mcc);
};
std::istream& operator>>(std::istream& input, Mutation_Code_Cost& mcc)
{
input >> mcc.MutCode;
input >> mcc.MutCost;
return input;
}
struct Gene
{
Gene_Code_Type geneCode;
Mutation_Code_Cost mutation_info[2];
friend std::istream& operator>>(std::istream& input, Gene& g);
};
std::istream& operator>>(std::istream& input, Gene& g)
{
input >> g.geneCode;
input >> g.mutation_info[0];
input >> g.mutation_info[1];
return input;
}
You can the read from the file like so:
std::vector<Gene> database;
Gene g;
std::string record;
while (std::getline(input_file, record))
{
std::istringstream record_stream(record);
if (record >> g)
{
database.push_back(g);
}
}

How to set class objects from vector of string in c++

These are the data in my Login.csv file:
ID,Name,Password,Gender
1,Liam,1234,M
2,Janice,0000,F
So probably I'll use class & objects to create login details, and write it into the file. After that I will split the csv from file into a vector of strings, from there how do I load back the details to objects of class.
This is my code of splitting the csv from file:
int _tmain(int argc, _TCHAR* argv[])
{
string line;
ifstream fin("users.csv");
while (getline(fin, line)){
vector<string> token;
split(line, ',', token);
for (int i = 0; i < token.size(); i++){
cout << token[i] << " ";
//////////// <<here>>
}
cout << endl;
}
system("pause");
return 0;
}
void split(const string& s, char c, vector<string>& v) {
string::size_type i = 0;
string::size_type j = s.find(c);
while (j != string::npos) {
v.push_back(s.substr(i, j - i));
i = ++j;
j = s.find(c, j);
if (j == string::npos)
v.push_back(s.substr(i, s.length()));
}
}
I was thinking how can I set the splitted strings from the string vector to a vector of objects, something like this: (to put in the << here >> section i commented in above)
vector<Login>loginVector;
//all the objects below should set from string vector (token)
loginVector[i].setID(); //i=0, id=1, name=Liam, password=1234, gender=M
loginVector[i].setName();
loginVector[i].setPassword();
loginVector[i].setGender();
loginVector[i].setID(); //i=1, id=2, name=Janice, password=0000, gender=M
loginVector[i].setName();
loginVector[i].setPassword();
loginVector[i].setGender();
Thank you.
Implement your Login object and populate it in the loop.
struct Login {
enum Gender {
Male,
Female
};
int Id;
std::string Name;
std::string Password;
/* you should probably use a constructor */
};
/* to construct your vector */
int I = 0;
while(I < token.size()) {
/* in your iterator */
Login& LO = loginVector.emplace_back(Login{});
LO.Id = std::stoi(token[++I]);
LO.Name = token[++I];
/* etc...*/
}
Note that this assumes your CSV is well formed, up to you to implement all the checking and make sure you handle corner cases like possible errors from stoi, blank rows or missing columns.
Also, don't do system("pause");, you're executing a shell to sleep for you, which has massive overhead compared to just using sleep which does literally the same thing except in a far more direct way.
I personally would implement this by adding an extraction operator for your class.
You'll have to friend the extraction operator because it must be defined externally to your class, since it's actually operating on the istream and not on your class, so something like this should be defined in your class:
friend istream& operator>> (istream& lhs, Login& rhs);
Depending on how your variables are named your extraction operator should look something like this:
istream& operator>> (istream& lhs, Login& rhs) {
char gender;
lhs >> rhs.id;
lhs.ignore(numeric_limits<streamsize>::max(), ',');
getline(lhs, rhs.name, ',');
getline(lhs, rhs.password, ',');
lhs >> ws;
lhs.get(gender);
rhs.isMale = gender == 'm' || gender == 'M';
return lhs;
}
Live Example

Remove entire rows with missing values c++

I am reading the data with different variables by the following codes, currently when the program touches missing values (represented in data by string "NA", it will change them to zero. Alternatively, I wonder if how can we remove entire rows when program touch "NA". I have tried to look for the same question but they all are for R, not C++. Please, if you can give me some advises. Thanks
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
struct Data {
vector<double> cow_id;
vector<double> age_obs;
vector<double> dim_obs;
vector<double> my_obs;
vector<double> mcf_obs;
vector<double> mcp_obs;
vector<double> mcl_obs;
vector<double> bw_obs;
vector<double> bcs_obs;
double get_number (string value)
{
if (value == "NA")
{return 0.0;}
else
{
istringstream iss (value);
double val;
iss>>val;
return val;
}
}
void read_input (const string filepath)
{
ifstream data_in (filepath.c_str());
if (!data_in)
{cout<<"Failed to open"<<endl;}
else
{
// Read tokens as strings.
string id, age, dim, my, mcf, mcp, mcl, bw, bcs;
string dummy_line;
getline(data_in, dummy_line);
string line;
while (data_in >> id >> age >> dim >> my >> mcf >> mcp >> mcl >> bw >> bcs)
{
// Get the number from the string and add to the vectors.
cow_id.push_back(get_number(id));
age_obs.push_back(get_number(age));
dim_obs.push_back(get_number(dim));
my_obs.push_back(get_number(my));
mcf_obs.push_back(get_number(mcf));
mcp_obs.push_back(get_number(mcp));
mcl_obs.push_back(get_number(mcl));
bw_obs.push_back(get_number(bw));
bcs_obs.push_back(get_number(bcs));
}
data_in.close();
}
size_t size=age_obs.size();
for (size_t i=0; i<size; i++)
{
cout<<cow_id[i]<<'\t'<<age_obs[i]<<'\t'<<dim_obs[i]<<'\t'<<my_obs[i] <<'\t'<<mcf_obs[i]<<'\t'<<mcp_obs[i]<<'\t'<<mcl_obs[i]<<'\t'<<bw_obs[i] <<'\t'<<bcs_obs[i]<<endl;
}
};
int main()
{
Data input;
input.read_input("C:\\Data\\C++\\learncpp\\data.txt");
}
Let's talk tables here.
Tables are containers of records (rows). The data you are capturing from your input file is already organized into records. So the obvious model is to use a structure that matches your file's data records.
struct Record
{
unsigned int cow_id;
unsigned int age_obs;
unsigned int dim_obs;
// ...
};
Your table could be represented as:
std::vector<record> my_table;
So to remove a record from the table, you can use the std::vector::erase() method. Easy. Also, you can use the std::find() function to search the table.
Let's relieve some reader's headaches with your present code by introducing a concept of the record loading its members from the file.
Reading a record from a file is best performed by overloading the stream extraction operator>>:
struct Record
{
//...
friend std::istream& operator>>(std::istream& input, Record& r);
};
std::istream&
operator>>(std::istream& input, Record& r)
{
std::string record_text;
std::getline(input, record_text);
// Extract a field from the record text and check for NA,
// Assign fields of r to those values:
r.cow_id = value;
// Etc.
return input;
}
With the overloaded operator, your input looks like:
Record r;
while (input_file >> r)
{
table.push_back(r);
}
Elegant and simple (reducing injection of defects).

Storing lines of input into a vector

In my program I am trying to take from the user lines of input actually names then storing them into a vector.
I wrote my own code but I got a runtime error telling me that "string subscript out of range".
This is my code
const int LEN = 100;
struct Case{
public:
int No_People;
vector<string> Names;
vector<string> Results;
void Set_Data(){
cin >> No_People;
int Size = No_People;
char Line[LEN];
for (int i = 0; i < Size; i++){
cin.getline(Line, LEN);
Names.push_back(Line);
}
}
}
Personally I would define a class to represent a line. Then you can use stream iterators to load the vector.
class Line
{
std::string line;
public:
// Operator to convert a line back to a std::string
operator std::string const&() const {return line;}
// Friend function to read a line from a stream.
friend std::istream& operator>>(std::istream& in, Line& data)
{
return std::getline(in, data.line);
}
};
int main()
{
int countOfPeople;
std::cin >> countOfPeople;
std::vector<std::string> lines;
std::copy_n((std::istream_iterator<Line>(std::cin)), countOfPeople,
std::back_insert_iterator(lines));
}
There's no need to use a char[] array, use std::string instead, especially given that you already are using it.
Note to OP: cin.getline() is this one:
std::istream::getline(char*, int)
The one you ned to use for std::string's is this one:
std::getline(istream&, string&)
struct Case{
public:
int Size;
vector<string> Names;
vector<string> Results;
void Set_Data(){
std::string temp;
cin >> Size; cin.ignore();
for (int i = 0; i < Size; i++){
std::getline(cin, temp);
Names.push_back(temp);
}
}
}
As far as compile errors go, always:
quote the exact error messgae
tell the line it happened at
show the code that contains the line and the relevant classes/methods
Most probably you are accessing the string using subscript which is out of index. It will be easy to answer if you point at which line you are getting the error.

Assigning file data to array of structs

I'm currently attempting to assign a line of data from an input file to an array of structs.
Here is my struct:
struct student
{
int ID;
int hours;
float GPA;
};
student sStudents[MAX_STUDENTS]; // MAX_STUDENTS = 10
Where:
for (int i = 0; !inputFile.eof(); i++)
{
getline(inputFile, dataLine);
cout << dataLine << endl; // Everything outputs perfectly, so I know dataLine is getting the correct information from getline()
//??
}
After an hour of crawling through Google I still don't have an idea of how to get my getline() data into each struct array.
I have tried,
sStudents[i] = dataLine;
sStudents[i] << dataLine;
sStudents.ID = dataLine;
Here is my data file:
1234 31 2.95
9999 45 3.82
2327 60 3.60
2951 68 3.1
5555 98 3.25
1111 120 2.23
2222 29 4.0
At this point I've become frustrated and I'm just not sure what to do. I'm convinced at this point I'm going about it completely incorrectly but not sure how to continue from here. I know that 10 elements of sStudents exist so that's good but how can I get the values from the input file into each .ID, .hours, .GPA? Perhaps getline() is being used incorrectly here?
You can simply do the following:
int ID = 0;
int hours = 0;
float GPA = 0.0;
int i = 0;
ifstream inputFile("data.txt");
while (inputFile >> ID >> hours >> GPA)
{
sStudents[i].ID = ID;
sStudents[i].hours = hours;
sStudents[i].GPA = GPA;
i ++;
}
A suggestion using the Standard Library.
#include<iostream>
#include<fstream>
#include<vector>
// your data structure
struct Student {
int id;
int hours;
float gpa;
};
// overload the input stream operator
bool operator>>(std::istream& is, Student& s) {
return(is>>s.id>>s.hours>>s.gpa);
}
// not necessary (but useful) to overload the output stream operator
std::ostream& operator<<(std::ostream& os, const Student& s) {
os<<s.id<<", "<<s.hours<<", "<<s.gpa;
return os;
}
int main(int argc, char* argv[]) {
// a vector to store all students
std::vector<Student> students;
// the "current" (just-read) student
Student student;
{ // a scope to ensure that fp closes
std::ifstream fp(argv[1], std::ios::in);
while(fp>>student) {
students.push_back(student);
}
}
// now all the students are in the vector
for(auto s:students) {
std::cout<<s<<std::endl;
}
return 0;
}
To get data out of an input stream use the >> operator. So:
int i;
file >> i;
extracts a single integer from the file. By default it is space delimited. Use that and see if you get further.