Search a file and output specific lines

Hello! So I am making a contact manager and I am using a text file to hold some information on the contacts the user inputs. I need to search this file and output the name, email(s), and phone number(s) of the contact. I understand how to write and read from/to a file and I can search a file for one line of code but I'm having difficulties searching this file and outputting several lines of code. Here is an example of what my text file looks like after some user input

John Doe
theblank@email.com
theblank@email.com
555-555-5555


So I need to have the user search this file by inputting the name John Doe and then the program should output the following three lines. The amount of emails and phone numbers the user inputs is tied to a vector so that is not a set amount. Therefore, the search must be able to flux between outputting just a few lines of code to many. For example, a contact could look like this.

John Doe
theblank@email.com
theblank@email.com
theblank@email.com
555-555-5555
555-555-5555

So I need to figure out how to write a search algorithm for a text file to search for a name and output the rest of the information about the contact. The amount of information can vary. I don't think any code is necessary here but if you want to take a look I can post it.

I greatly appreciate any and all help!

What separates contacts?

A blank line?
A name line following a phone number line?
A special character?

If you have control over the file format, you can make it whatever you find convenient. A blank line between records is very, very convenient.


Thereafter, all you need to do is read the file until you find the contact name, then print the name, then read and print lines until you read a blank line (or whatever separates records) and quit.

Hope this helps.

Sorry for not mentioning that. They are just separated by a blank line. That's what I was thinking but I'm not sure how to implement it.

So read the file while it's true that there are characters? How would I code the program to recognize a blank line? Should I put in a flag, possibly a tildy (~) or something between contacts?
Here's a simple demo to get you started:
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

int main()
{
  ifstream src("contacts.txt");
  if (!src)
  {
    cerr << "Error opening file\n";
    return EXIT_FAILURE;
  }

  cout << "Enter name to search for: ";
  string search_name;
  getline(cin, search_name);
  src >> noskipws;
  string input_line;
  
  while (getline(src, input_line))
  {
    if (search_name == input_line)
    {
      cout << input_line << '\n';
      while (getline(src, input_line))
      {
        if (input_line.empty())
          break;
        cout << input_line << '\n';
      }
    }
  }
}


Input file:
John Doe
theblank@email.com
theblank@email.com
theblank@email.com
555-555-5555
555-555-5555

Jill Doe
jilldoe@email.com
666-666-7777

Jane Doe
janedoe@email.net
888-999-888


Output:
Enter name to search for: Jill Doe
Jill Doe
jilldoe@email.com
666-666-7777


// For you to do:
* add a loop to allow multiple searches
* some output in case not found
Ok, thanks so much! I'm trying to implement this now. I'm going to spend a few minutes figuring it out and then there won't be any issues hopefully!

Now I have to figure out how to delete from a file, haha. Let's hope there's no issues there. I greatly appreciate the help!
Here's a slightly different way. It's more complex than Thomas1965's solution but it anticipates the need to read and write full records of a contact.

The idea is to create functions that read and write a single contact, and then test them by simply reading a set if contacts and writing them out again. Once you have this working, adding code to filter out some contacts is easy.
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
57
58
59
60
61
#include <iostream>
#include <vector>
#include <string>

using std::vector;
using std::string;
using std::cin;
using std::cout;


class Contact {
public:
    string name;
    vector<string> emails;
    vector<string> phones;
    bool read(std::istream &);
    bool write(std::ostream &);
};

bool
Contact::read(std::istream &is)
{
    string str;
    getline(is, name);		// read the name

    emails.clear();
    while(getline(is, str) && str.find('@') != string::npos) {
	emails.push_back(str);
    }

    phones.clear();
    while (is && str.size()) {
	phones.push_back(str);
	getline(is, str);
    }
    return is.good();
}

bool
Contact::write(std::ostream &os)
{
    os << name << '\n';
    for (string & email : emails) {
	os << email << '\n';
    }
    for (string & phone : phones) {
	os << phone << '\n';
    }
    os << '\n';
    return os.good();
}


int main()
{
    Contact c;
    while (c.read(cin)) {
	c.write(cout);
	cout << "--------------------\n";
    }
}

Interestingly enough, that's very similar to how I have my code set up dhayden. I actually already had the write/read stuff done but it's pretty similar to how you set it up. I really appreciate the reply though, I managed to clean up my code based on how you had it.

Thanks again, everyone!
Got everything working, thanks so much, everyone!
Topic archived. No new replies allowed.