Search txt file for line, then read in everything on that line to variables

Hi!
I'm writing a POS system (just for fun) and I have the prompt asking the cashier to input their customer number, and it should check the txt file for that number and read that line into several different variables.

EXAMPLE members.txt
2002 John Adams Q
5565 Quincy Gates L

Please enter the customer's member number: 5565

and the following would happen (ideally):

memberNumber=5565;
firstName=Quincy;
lastName=Gates;
middleInitial=L;

How would I go about doing that (preferribly using ifstream, if possible, unless there is an easier way)

Thank you in advance!

The code I'm currently using that is utterly botched is below:

1
2
3
4
5
6
7
        cout << "Please enter their membership number: ";
        cin >> memberNumber;
        ifstream memfile;
        memfile.open("members.txt");
        getline(memfile, memberNumber);
        memfile >> firstName >> lastName >> middleInitial;
        memfile.close();
One simple way would be to read your entire file once and store all the data into memory.
When the user inputs his number, you just have to look for it in the loaded data.

If you really want to search into your file, the code may be something 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
int num;
std::string fname, lname, initial;

// read while you haven't reached the end of the file and memberNumber has not been found
do{
    memfile >> num >> fname >> lname >> initial;
}while( memfile.good() && num != memberNumber );

if( memfile.fail() || num != memberNumber )
{
    // the number was not found, handle the case
}
else
{
    memberNumber = num;
    firstName = fname;
    lastName = lname;
    middleInitial = initial;
}

// if you want to keep the stream open between searches, clear the error flags and reposition it to the beginning of the file
memfile.clear();
memfile.seekg(0, ios::beg);


If you want your code to be cryptic, you can write the do/while loop this way:
 
while( memfile >> num >> fname >> lname>> initial && num != memberNumber ) {;}
That works, except that it tells me that "error: no match for 'operator!=' in 'num != memberNumber'|
error: no match for 'operator!=' in 'num != memberNumber'|
||=== Build finished: 2 errors, 0 warnings ===|
"
when I compile. Any ideas?
I assumed memberNumber was an int. Are you by chance using a string ?
If that's the case, define num as a std::string too.
I had a similar issue to this recently. Try something along these lines maybe?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <vector>
#include <sstream>

while (memfile.good())
{
string line;
stringstream iss(line);
string buf;
vector<string> tokens;
while(iss >> buf)
{
	tokens.push_back(buf);
}
for (int i = 0; i<tokens.size(); i++)
{
      cout << token.at(i);
}
}


You would then search the data inside of token vector for the number and you could make it output the name.

I wrote this out very quickly so if there are any errors I'm sorry!.
@Hucaru : when are you reading from memfile ?
And you have to check the validity of the stream after you've read from it and before you store the data, or your last acquired value could be garbage.
Last edited on
My bad I forgot the getline(memfile,line); part. Could you please elaborate on checking the validity of the stream? First time I have heard of it.
It's simple : the stream can only report a failure after it happened.

The problem with your loop is that you read data, which can cause a failure, and store it immediatly without checking the stream before.

If ever one of the reads causes an error, the acquired value is very likely to be garbage. With your code, you would store this value into your vector and stop the loop only at the next iteration.

To avoid that, you must:
1) read from the stream into temporary variables
2) check the validity of the stream
3) if the stream is valid, store/use the acquired data.

more details:
http://gehrcke.de/2011/06/reading-files-in-c-using-ifstream-dealing-correctly-with-badbit-failbit-eofbit-and-perror/
http://www.parashift.com/c++-faq-lite/stream-input-failure.html
http://www.parashift.com/c++-faq-lite/istream-and-eof.html
Thanks. I understand what you mean now. I think I am going to have to practice this as the code seems a bit confusing at the moment hahaha.
Topic archived. No new replies allowed.