Reading Data from a File

Hi all! I'm kind of stuck here. I'm creating a program that accepts a name or number as input from the user and is supposed to search and find that name or number in a text file and print it out.

I created a program with a string that holds the names and numbers and this works properly, but not sure how to alter the program so it reads the data from a text file instead of the array I declared.

Any help or advice would be much appreciated. Thank you!

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
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

int main()
{
	
/*string phoneList[]={"Becky Warren, 678-1223",
	"Joe Looney, 586-0097",
	"Geri Palmer, 223-8787",
	"Lynn Presnell, 887-1212",
	"Holly Gaddis, 223-8878",
	"Sam Wiggins, 486-0998","Bob Kain, 586-8712",
	"Tim Haynes, 586-7676",
	"Warren Gaddis, 223-9037",
	"Jean James, 678-4939",
	"Ron Palmer, 486-2783"};
*/

	const int SIZE = 11;
	string input;
	bool found = false;

        ifstream file;
        file.open("phoneList.txt");


	cout<<"Enter a name or phone number to search:\t";
	cin>> input;

	for(int i = 0; i < SIZE; i++)
	{

		if(phoneList[i].find(input)!=string::npos)
		{
			found=true;
			cout << phoneList[i]<<endl;
		}
	}
	if(!found)
        cout << "Name not found.\n";

        file.close();
	return 0;
}
Try reading each entry into a struct and storing each record into a specialised class or a vector would be easier.

Altering a file directly is much harder work than changing entries in memory and writing the memory contents out to save it.

@megatron0 Thanks for the quick response! We haven't covered structs yet so I'm curious if there's another way?

If I wrote the array out in the text file could I work through it that way?
Give me a sec, I'll write out some code. I don't know if there is another way. If you haven't covered structs I doubt you haven't covered file stream pointers, which is what you would need to alter a file directly, which is a big pain in ass.
The unit is on c-strings
Would this work?

ifstream file("phoneList.txt");
string input;

while(getline(file, input)) {


}
Last edited on
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
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <string>
#include <fstream>

struct Record // A struct is a data structure, it contains various pieces of data
{
	unsigned short AreaCode;
	unsigned short LocalCode;
	std::string FirstName;
	std::string LastName;
};


std::istream& operator>>(std::istream& is, Record& r) // This function is used so we can std::ifstream >> Record
{
	is >> r.FirstName >> r.LastName >> r.AreaCode >> r.LocalCode;
	return is;
}

std::ostream& operator<<(std::ostream& os, Record& r) // This function is used so we can std::cout << Record
{
	os << r.FirstName << " " << r.LastName << " " <<  r.AreaCode << " " << r.LocalCode;
	return os;
}


bool ReadFile(Record r[], const int sz, const char* filename) // Read sz amount of records into r
{
	std::ifstream file;		// We open the file as normal;
    file.open(filename);

    if(file)				// Check if the file was opened
    {
    	for(int i = 0; i < sz; i++) // Loop through and read the records
    	{
    		file >> r[i];
    	}
    	return 1;	// File read
    }
    else
    {
    	return 0; 	// File could not be opened
    }
}


int main()
{
	const int SIZE = 11;

	Record PhoneBook[SIZE]; // We create an array of records to represent the phonebook

    // Now we read the file into the struct

    if(ReadFile(PhoneBook, SIZE, "records.txt"))
    {
    	std::cout << "File opened and read into array.\n\n";
    	for(int i = 0; i < SIZE; i++) std::cout << PhoneBook[i] << "\n";

    	// Now it is your job to write an interface for the user
    	// Think of solutions like

    	// for(int i = 0; i < SIZE; i++)
    	// {
    	//		if(PhoneBook[i].FirstName == input) <-- Record found, do whatever with it

    }
    else
    {
    	std::cout << "File could not be opened.\n\n";
    }

	return 0;
}


Tiny edit:

here is the record.txt file, it easy to extract data that is separated by a space or tab

records.txt


Becky Warren	678 1223
Joe Looney		586 0097
Geri Palmer		223 8787
Lynn Presnell	887 1212
Holly Gaddis 	223 8878
Sam Wiggins	486 0998
Bob Kain		586 8712
Tim Haynes		586 7676
Warren Gaddis	223 9037
Jean James		678 4939
Ron Palmer		486 2783
Last edited on
Wow! This looks pretty complicated, I appreciate all your help! Many thanks!
Of course you can ignore the operator overloading, but you will have to do what's in the function manually.

Surprisingly, it's not that hard. It may look like a lot, but all you do is define what a record is, write a few lines that reads a file into an array of records ( a PhoneBook ), the rest is up to you.

I hope you can learn something to help from that.


EDIT:

If you purely just want to read the file to see if a record exists then:

1
2
std::getline(file, line);
if(line.find(input) != std::npos) // The data item exists within the line 
Last edited on
Topic archived. No new replies allowed.