Read from file specific words/strings.

Hello,

Im trying to write a program that lists only the users names which are stored inside a text file and also displays the numbers of users in it.

The format of my txt file is:

id name birthdate

for example:

1
2
3
200 john birthdate
301 casey birthdate
204 leo birthdate



In this case the output would be:
3 users found
and the users names only.

So far I've been able to count the numbers of users with the follow code:


As for listing the users names I simply dont know how to do it. I know getline is able to read the entire line but in this case Im just trying to read the second word of each line from the text. Bare in mind Im very new to cpp.

Thanks.
Last edited on
You can read the input one chunk at a time. So read everything, and just don't keep what you don't want. Something like this.

1
2
3
4
5
6
7
8
9
10
11
int id;
string name;
string birthdate;
int count = 0;

while (text >> id >> name >> birthdate)
{
  cout << "Username : " << name << endl;
  count++;
}
cout << "Users found:" << count << endl;  


Topic archived. No new replies allowed.