How to read a list of data from a text file?

Jun 1, 2013 at 10:40pm
So right now Im trying to read data in from a text file. I got a text file that basically looks like this:

John 15 5 1


Frank 23 8 1


Zach 17 1 4


John would be the first name, 15 would be an int, 5 would be an int, and 1 would be an int. How can I use getline to store each each value in that order, then go to the next line and do it again, and then go to the next line and do it again, until the end of file?


so far I have this

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

#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int main()
{
	ifstream employees;
	string lastname, firstname, lastname1, firstname1, lastname2, firstname2;
	float base, sales, base1, sales1, base2, sales2;
	int years, years1, years2;

	



	employees.open("employees.txt");

	employees >> lastname >> firstname;

	cout << lastname << firstname;

	return 0;
}


So I understand how to read from this first line, but I dont know how to read the next line? Also I havnt really learned arrays yet, so I would prefer to solve this without them if possible.
Last edited on Jun 1, 2013 at 11:03pm
Jun 2, 2013 at 12:01am
I suggest using strings to store the entire file and go from there. There is a function for almost everything:

http://www.cplusplus.com/reference/string/string/find/

http://www.cplusplus.com/reference/string/string/substr/

http://www.cplusplus.com/forum/general/13135/

There might be a better way to do this however.
Jun 2, 2013 at 12:07am
strings are easier to work with than ifstreams, so you should probably convert it to a string.

http://www.cplusplus.com/reference/string/string/getline/

For your delimitation character use '\0' which pretty much means end of file.

Also I havnt really learned arrays yet, so I would prefer to solve this without them if possible.


You might want to read/learn more before doing this, unless this is your homework or something.
Last edited on Jun 2, 2013 at 12:08am
Jun 2, 2013 at 12:21am
I am bored. This will help you:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <fstream>


using namespace std;

int main()
{
	ofstream iambored("hello.txt");
	iambored<<"Matthew 14 4";
	iambored.close();
	ifstream ihatemathhomework("hello.txt");
	string dontwannadoit;
	getline(ihatemathhomework,dontwannadoit , '\0');
	string thisismatthew=dontwannadoit.substr(dontwannadoit.find("Matthew"),7);
	cout<<thisismatthew;
        ihatemathhomework.close();


	return 0;
}
Last edited on Jun 2, 2013 at 12:22am
Jun 2, 2013 at 12:35am
You might also want to invest into structures.
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
#include <iostream>

struct Info {
    std::string first_name , last_name;
    double base , sales;
    short years;
};

std::ostream& operator<< ( std::ostream &stm , const Info &inf )
{
    return( stm << "Employee - " << inf.first_name << " " << inf.last_name
                << "\nBase     - " << inf.base
                << "\nSales    - " << inf.sales
                << "\nYears    - " << inf.years );
}
int main()
{

    Info employees[ 2 ];

    employees[ 0 ].first_name = "Gib";
    employees[ 0 ].last_name = "Lit";
    employees[ 0 ].base = 12.34;
    employees[ 0 ].sales = 56.78;
    employees[ 0 ].years = 90;

    employees[ 1 ] = { "Lit" , "Gib" ,
    43.21 , 87.65 , 9 };

    std::cout << employees[ 0 ] << std::endl << employees[ 1 ] << std::endl;
    
    return( 0 );
}
Topic archived. No new replies allowed.