Reading a file line by line and storing the data in an object.

Hello, I am working on an assignment that requires me to read a .txt file given by an argument on the command line -- a "Student" file that contains information about students -- and store the information in a "Person" class.

Each part of an entry in the Student file is given on a separate line in the file. Student ID is first, name is second, address is third, and phone is last. There are no blank lines between students. The following is an example file.

529173860
Dick B. Smith
879 Maple Road, Centralia, Colorado 24222
(312) 000-1000
925173870
Harry C. Anderson
635 Main Drive, Midville, California 48444
(660) 050-2200


I have already written a "Person" class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Person {
public:
	string ID;
	string name;
	string address;
	string phone;

	//constructor:
	//student ID
	//name
	//address
	//phone
	Person(string ID0, string name0, string address0, string phone0) {
		this->ID = ID0;
		this->name = name0;
		this->address = address0;
		this->phone = phone0;
	}


Which appears to be working fine. And I know how to read lines of a file using getline(), but my problem comes when I try to read four lines and then after that, store those four lines as members of a "Person". This is what I have so far:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main (int argc, char* argv[]) {
    
        ifstream studentFile(argv[1]);
	//creates a filestream from the first argument given on the command line 
	//file is called "studentfile"
	
	
	if (studentFile.is_open()) { //checks to see if file is good and that it opened correctly
		
        while(!studentFile.eof) { //while we are not at the end of the file
		string myString;
	        getline(infile, myString); // Saves the line in myString
	        }


I am aware that all this does is store each line as a string and then not really do anything with it. I guess I'm just a little confused because I only really know how to work with one line at a time, not four.

What I need to do is store line 1 as the ID, line 2 as the name, line 3 as the address, and line 4 as the phone number of a new instance of class Person. (then maybe store each person in a vector of persons). I can write "set" functions in my person class if necessary, but I was thinking there might be a way to just construct an instance of person using the four lines. Any help would be much appreciated. Thank you.
Last edited on
Give your class an operator>>, make it easy.

While at it, I'll fix a couple style errors

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
#include <string>
#include <sstream>
#include <iostream>
class Person {
    std::string ID;
    std::string name;
    std::string address;
    std::string phone;
public:
        Person(std::string ID0, std::string name0, std::string address0, std::string phone0)
    : ID(ID0), name(name0), address(address0), phone(phone0) {} // proper syntax
    Person() {}

    void show() const {
            std::cout << "Person created with ID " << ID << " and phone " << phone << '\n';
    }

friend std::istream& operator>>(std::istream& is, Person& p) 
    {
        getline(is, p.ID);
        getline(is, p.name);
        getline(is, p.address);
        getline(is, p.phone);
        return is;
    };
};

int main()
{
    std::istringstream test("529173860\n"
                            "Dick B. Smith\n"
                            "879 Maple Road, Centralia, Colorado 24222\n"
                            "(312) 000-1000\n"
                            "925173870\n"
                            "Harry C. Anderson\n"
                            "635 Main Drive, Midville, California 48444\n"
                            "(660) 050-2200");
    Person p;
    while(test >> p) // proper input loop
    {
        p.show();
    }
}
demo: http://ideone.com/h3Hse

note that this makes no attempt to validate input; a stray empty line and the rest of the file is processed incorrectly. Consider reporting an error if the ID is not an integer or the phone number doesn't match an expected phone number layout.
Last edited on
Topic archived. No new replies allowed.