Finding specific text in a file

I have made a file that contains a list of people.
Now what I wanted to do was that if I enter a number, I will get the Person corresponding to the person.

This is the relevant part of the code:

1
2
3
4
5
6
7
std::ofstream ScoreInput ("UserScore.txt",std::ios::out | std::ios::app);

	ScoreInput << IDNumber;		//Person's ID
	ScoreInput << FullString;	//The Combined String
	ScoreInput << Blank;		//The Carriage return
	
ScoreInput.close();


Here Blank and FullString are two strings. Blank is a carriage return ("\r") while the FullString is a combined string consisting of a number of facts about the person (Age, Height...)
The Facts are separated by a newline and they have a tab before they are written into the file.

So is there a way to enter a number, then have the computer check against every
IDNumber until it finds a match?
The way that I would do it, is create a method that reads in the entire log file into a member variable (string). You could use ifstream.

Then have a separate method that parses the individual "Person" entries from the member variable that holding the entire log into a Vector of "Person" objects or structs (which ever is more appropriate).

Then in your search routine, using IDNumber as the search term, iterate through all "Person" objects until the desired one is found.

This method might not be the fastest or most efficient, but I think it is probably the simplest to implement.

I hope this helps!
Last edited on
Then have a separate method that parses the individual "Person" entries from the member variable that holding the entire log into a Vector of "Person" objects or structs (which ever is more appropriate).


Could you give me some reference about paring and Vectors?
Here are the references from this site about pair's and vector's.

pair: http://www.cplusplus.com/reference/std/utility/pair/
vector: http://www.cplusplus.com/reference/stl/vector/

But I'm not sure why you want Pairs?
Oh! it was a typo, I was asking about parsing!!
The parse routine will largely depend on the formatting of the file containing the data.

The simplest way (in my opinion), would be to read in the entire source file into a string object, then use string objects methods such as string::find(...), string::substr(...) and string::compare(...), to perform the search and retrieve operations.

More information about methods:

string: http://www.cplusplus.com/reference/string/string/
string::find(...): http://www.cplusplus.com/reference/string/string/find/
string::substr(...): http://www.cplusplus.com/reference/string/string/substr/
string::compare(...): http://www.cplusplus.com/reference/string/string/compare/

Here is a Wikipedia article that outlines what Parsing is: http://en.wikipedia.org/wiki/Parsing
Last edited on
The File looks like this:

1.
Name: Person

Age: 25

Height: 180 cm


Now the program ends with a "\r". So I can use that as the delimiter while reading the string.

That aside, tell me if I am correct:
I can use find to find the pos of the IDNumber, and then use the substr function to get the desired text?
Nisheeth wrote:
I can use find to find the pos of the IDNumber, and then use the substr function to get the desired text?
Correct.
i'm a little bit confused about the use of struct. what's the meaning of this:

template <class T1, class T2> struct pair;

i'm a little bit about many variations about the use of struct.
Here it is simple example...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
string myString;
string Finder;

getline(cin, myString);
getline(cin, Finder);


string::size_type tip1 = find(Finder, 0);

if(tip1 != string::npos){

cout<<"The word "<<Finder<<is found at position<<tip1<<endl;
}

else 
       cout<<"Could not find the word: "<<Finder<<endl;


Last edited on
chipp wrote:
i'm a little bit confused about the use of struct.

A struct is a way to group different kinds of data, usually when they have something in common.

For example a Name, Age and Telephone Number are different kinds of data, however the commonality in them is that they could belong to a Person, and thus it would make sense they could be stored within a structure called Person. For example:

1
2
3
4
5
6
struct Person
{
    string  sName;
    int       nAge;
    string  sTelNo;
};


More on structures can be found here: http://www.cplusplus.com/doc/tutorial/structures/

chipp wrote:
template <class T1, class T2> struct pair;


This line specifies the format of the prototype of the "pair" class, where class T1 & T2 are two potentially different classes.

This format is derived from Templates and more information about them can be found here: http://www.cplusplus.com/doc/tutorial/templates/

This was a little off topic but I hope this clears it up for you.
oh, i'm don't really know about the template function (a little bit forgot and a little bit lazy to search it :D ). i understand now, it just the template for struct pair it just written in one line. i got it
Topic archived. No new replies allowed.