Problem with reading from a file and a c-string

This is the line the I'm trying to read from the "master" file:

5 Christine Kim (spaces) 30.00 2 1 F


And this is the code that I'm using:
1
2
3
4
5
6
7
master >> masId;                          //int
	master.ignore();
	master.getline(empName, 21);        //char [21]
	master >> empHourlyPay;               //double
	master >> empNumDeps;               //int
	master >> empType;                      //int (either 1 or 0)
        master >> struct[0].gender;           //char - employee gender 


however, when I try to output the various values to check my progress, I only get zeros for everything after 'empName.'

why?
According to the getline reference,
http://www.cplusplus.com/reference/iostream/istream/getline/

Parameters

n

Maximum number of characters to store (including the terminating null character).
This is an integer value of type streamsize.
If the function stops reading because this size is reached, the failbit internal flag is set.

I'm assuming the "Christine Kim (spaces)" is 21 characters. If so, master's failbit is being set because it's reading all the characters before finding the newline. You need to call master.clear() to proceed further in this case. Perhaps you should be using master.read() instead. Also, ensure that empName has a null terminator.
So I changed my code to this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
master >> masId;
		master.ignore();
		master.get(empName, 22);
		master.clear();
		master >> empHourlyPay;
		master >> empNumDeps;
		master >> empType;
		master.ignore();
		master >> check[0].sex;
		info[0].set(masId, empName, empHourlyPay, empNumDeps, empType);
		trans >> transId;
		trans >> check[0].hours;
		master.ignore();

		cout << masId << endl;
		cout << empName << endl;
		cout << empHourlyPay << endl;
		cout << empNumDeps << endl;
		cout << empType << endl;
		cout << check[0].sex << endl;


This is what it gave me:
5
Christine Kim #
30.00
3
1
1
5


Here is the file I'm reading from:
5 Christine Kim 30.00 2 1 F
15 Ray Allrich 10.25 0 0 M
16 Adrian Bailey 12.50 0 0 F
17 Juan Gonzales 30.00 1 1 M
18 Morris Kramer 8.95 0 0 M
22 Cindy Burke 15.00 1 0 F
24 Esther Bianco 10.25 0 0 F
25 Jim Moore 27.50 3 1 M
32 Paula Cameron 14.50 0 0 F
36 Melvin Ducan 10.25 0 0 M
37 Nina Kamran 30.00 1 1 F
38 Julie Brown 35.00 0 1 F
40 Imelda Buentello 14.50 0 0 F
42 J. P. Morgan 12.50 0 0 M
43 Maria Diaz 15.00 0 0 F


When running the debugger on my compiler, apparently, get() is reading the entire line and not just the 20 characters it is supposed to.
or I could be dumb as a rock and read from the wrong file.

that master.close(); fixed everything... thanks
Topic archived. No new replies allowed.