I have written into a text file using a string vector of vectors. Now i want to read a user record (each has fifteen rows) using the below code, the problem is, the "cout" stream will not display the data_set vector:
Error message at line 43:
1>.\RefinedDatabase.cpp(338) : error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::vector<_Ty>' (or there is no acceptable conversion)
void read_file (std::fstream& opener,std::vector<std::string>& row ,std::vector< std::vector<std::string> >& data_set); // function prototype
int main()
{
int nrows;
cout<<"Enter the number of row records you want to create":
cin>>nrow;
std::fstream myFile ("Testing.txt", ios::in |ios::out |ios::app);
std::vector< std::vector<std::string> > data_set ; //takes row by row of a record from vector "row"
std::vector<std::string> row ; //receives user strings which are then copied into data_set
while( data_set.size() < nrows && get_row( std::cin, row ) ) //i have not included the get_row function here but i have defined it to fill the "row" vector
data_set.push_back(row) ;
read_file (myFile, row , data_set);
}
void read_file (std::fstream& opener,std::vector<std::string>& row ,std::vector< std::vector<std::string> >& data_set) {
//opener.open ("Testing.txt", ios::in);
string input;
for( std::size_t i = 0 ; i < data_set.size() ; ++i )
{
for( std::size_t j = 0 ; j < row.size() ; ++j )
while (getline (opener,input));
const std::vector<std::string>& row = data_set[i] ;
cout<<data_set.at(i);
}
}
Will that function read the record from the text file that appears like this (here below) ?
=======================================================
Lr Num: 3w75768uty
Proprietor name: Maina Mwangi
National Id Num: qwuitioyde
Proprietor address: eyoiyi
Kra PIN: eiouou
Date land was acquired: eyuu9iop
Date of title deed: ruoiiou
Category: reuiiuopu
Ownership type: ruiuiopi
County located: riuop[u
District: yuioopuop
Division: uropuipo
Location: jrioopuiop
Sub-location: siuoiuyoi
Village: uiyio
=======================================================
Lr Num: TR657687970M
Proprietor name: MWANGI ELIJAH
National Id Num: 57688798
Proprietor address: 345-0200 KENYA
Kra PIN: T576686889L
Date land was acquired: 19.06.2016
Date of title deed: 19.06.2016
Category: PRIVATE
Ownership type: ABSOLUTE
County located: NAIROBI
District: KASARANI
Division: GITHURAI
Location: G44
Sub-location: SECTION 3
Village: FLYOVER
=======================================================
I suppose rows represent 'records' here? I would suggest using a struct to make things less complicated.
1 2 3 4
struct Record
{
std::vector<std::string> rows;
};
With std::cout you're trying to print the type std::vector<std::string> which is the cause of the compiler error. You need to iterate through the vector and use std::string as the type.
Also if it's an option I'd suggest changing the file format to a table-like format. It'll reduce the file size and speed up the loading a fair bit. If that's a concern ofcourse.
AcarX....if i initialize the strings variables of structs using a vector like here below, how will i write them to a file and read them from file to display one record from a table format ? Have tried to do demonstrate how i think the struct way may work, should i do it like this ?
Similar to an excel table. This way when you're loading the file later on you could use a std::stringstream with std::getline(ss, col, '\t') which is much faster than reading each field line by line. Also if you use the structure you no longer need a rows variable unless you need it for later use. Besides file format all looks good.