Reading text data into vector of vectors

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) 



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

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) ?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
=======================================================
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
=======================================================


Last edited on
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.
Last edited on
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 ?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40

struct Record {
string Surname, OtherNames, IdNum, Location;    //etc

std::vector <std::string> rows;

};


void DisplayDetails (Record &create);

void Write_file (Record &create);

int main(){
Record create;

cout<<"EnterSurname";
getline (cin, create.Surname);

cout<<"Enter other names";
getline (cin, create.OtherNames); //and others....IdNum,Location, etc

rows.push_back (create);

DisplayDetails (create);

Write_file (create);
}

void DisplayDetails (Record& create){
cout<<"You have entered the following details:"<<endl;
cout<<"Surname :"<<create.Surname<<"Other names :"<<create.OtherNames<<endl;    //and others
}

Void Write_file (Record& create) {
ofstream writer ("Registration.txt", ios::app);

writer<<"Surname :"<<create.Surname<<" Other names: " <<create.OtherNames<<endl;   //and others 
}


Will the above snippet initialize the Record struct variables, then be able to display them and put the entered data to the text file ?
Last edited on
What I mean by table-like format is:
1
2
col1_description[tab]col2_description...
rec_1_col_1[tab]rec_1_col_2...

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.
Topic archived. No new replies allowed.