To read a specific portion of text file to a vector

I want to read the below sample of text file (a record at a time) when the user requests for one specific record .... how do i do it with the below function ? ...Or any other way known ?

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
49
50
51
52
53
54
55
56
void read_record (std::vector <std::string>& loader)
{
const int records_num = 30;          //one record has 2 "columns" and "15 rows/lines"

  ifstream reader ("Testing.txt");  //name of text file whose contents appear below


if (reader.fail ()) { 
	cerr<<"Error opening reader stream"<<endl; exit (1);
}


string indiv_details [records_num];     //a string variable which reader will read into, line by line


int i=0;



while (!reader.eof ()) 
{
	if ((i+1) % 2 ==0)   

//if (reader == "=================")       ------->wanted reader object to ignore "=====" from file

	//reader.ignore ();

		getline (reader, indiv_details [i++], '\n') &&      //or getline (reader, indiv_details = len. npos) ------------- ????

	
		getline (reader, indiv_details [i++], '\t');

}
i=0;


cout<<loader.push_back (indiv_details);   //read  rows/lines into vector





}

void main () {

   std::vector <std::string> loader;
  
	read_record (loader );

	system ("pause");
	
	EXIT_SUCCESS;
}




Sample contents of the text document

====================================================================================================================
Lr Num: 375768
Proprietor name: George Ngechu
National Id Num: jgjhi4507
Proprietor address: frjkfklj
Kra PIN: smkhklh
Date land was acquired: hklljlsd
Date of title deed: sjhkikt
Category: edhjoioiy
Ownership type: hyjkhs
County located: sjjhkhopieuoi
District: ueiiuoiuo
Division: sdhjhkhollj
Location: hkokeououo
Sub-location: shkkhojoj
Village: wiyiupo
====================================================================================================================
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
You have to read every record from the beginning of the file until you get to the one you're looking for. Text files in the general case don't permit random access.

A record-based text file can permit random access if every record is of a fixed length. To do this, you could pick a padding character (e.g. space) and a given length (e.g. 2048 characters). Records should not exceed that length, and shorter records should be padded with the padding character until they are exactly that length.
Then, to access record k, you just need to move the read file pointer to character/byte k*2048.
Helios, kindly demonstrate how i should do this :

1
2
3
4
5
6
7
8
9
10


A record-based text file can permit random access if every record is of a fixed length. 
To do this, you could pick a padding character (e.g. space) and a given length (e.g. 2048 characters). 

Records should not exceed that length, 
and shorter records should be padded with 
the padding character until they are exactly that length.
Then, to access record k, you just need to move the read file pointer to character/byte k*2048
Last edited on
I would recommend that you start by defining a structure/class to hold the record information. The read the file line by line filling in the record information and storing this information into a vector of your structure. Then you can search the vector for the proper record instead of searching the file.

Kindly peruse the post below please.
Last edited on
Does that code even compile?

What is with all of those cin.clear() calls?

It appears that you want to write the file, but I don't see where you actually write anything to the file.

It also appears that the loop in File_Writer() will never execute because the display vector is empty at the start of the function. I really don't see a purpose of the display vector in the code you provided.

It also appears that since you only ask the user for one set of data, that vectors are not required at this time.

Start simpler, get one set of data from the user (cin) then print this one set of data to the console (cout) to insure you understand what is going on. Then once you understand how to get and print one set of data you it should be easier to move to using the vector and getting multiple records from the user and printing those records.

Removed .... kindly clarify the post below
Last edited on
so i wanted a similar way to write the entered data using such a function i have posted above

Does that code even compile?

In your first snippet what is the purpose of all of those cin.clear() calls?

And you really really need to start consistently using a decent indentation style. Your code as presented is very difficult to read, proper indentation will help you be able to follow the program logic.
https://en.wikipedia.org/wiki/Indent_style
Ok, ...Jlb, i had at first mixed cin (for reading ints) and getline (for reading strings) but observed that after inputting long strings after cin and and getline interchangeably, some variables would skip. I therefore decided to flush both the cin stream and getline (i thought the getline may null terminate after several characters), but thank you i have removed the cin.clear and seen input is read ok.
Jlb, ....i have been visiting several links and have not seen any site demonstrating how to write vectors of struct to file.

But there are these two functions that the writer http://forums.codeguru.com/showthread.php?269648-C-Structure-How-do-I-write-a-structure-to-a-file" has not explained how they work, kindly explain how they work:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

struct str
{
  long  size;
  char* s;
};

#include <fstream>

void write_str( std::ofstream& out, str& s )
{
  out.write(reinterpret_cast<char*>(&s.size), sizeof(long));
  out.write(s.s, s.size * sizeof(char));
}

void read_str(std::ifstream& in, str& s)
{
  in.read(reinterpret_cast<char*>(&s.size), sizeof(long));
  s.s = new char[s.size];
  in.read(s.s, s.size * sizeof(char));
}


How can my LandDetails struct variables fit into these functions

so i may write ( their user-fed values) to a file ?

1
2
3
4
5
6
7
8
struct LandDetails 

{  // declaring a struct

string Surname,OtherNames,LandNum,KraPin, OwnerIdCard, address, LandCounty,LandDist, LandDiv,Landloc,LandSubloc;

}


How can i use the read_str () function to read the file i posted at the beginning of this thread ?

I will really appreciate to have those answers.
Last edited on
How can my LandDetails struct variables fit into these functions

Basically it can't. If you read the link you provided you should see reference to POD classes/structures. Your structure is not a POD type. Before you throw in the complexity of using the read()/write() functions you really should totally understand how to read and write "normal" text files. I suggest you start here:
http://www.cplusplus.com/doc/tutorial/files/



Topic archived. No new replies allowed.