luling1972
asked on
C++ reading text file extracting c-string using get function
I am attempting to read a record from a (.txt) file. The layout is:
int ID;
char name[21];
double Payrate;
int dependents;
I am using the code block below:
while (!masterFile.eof())
{
//get the next read of the file
masterFile >> ID;
masterFile.get( name, 21, '\n' );
masterFile>> payRate >> dependents >> eType;
numItems++;
}
The question is the "get" function does not work, it returns nothing. When this happens the rest of the read is messed up. Could someone tell me how to get this get to work. I must use the get function please do not suggest other methods.
int ID;
char name[21];
double Payrate;
int dependents;
I am using the code block below:
while (!masterFile.eof())
{
//get the next read of the file
masterFile >> ID;
masterFile.get( name, 21, '\n' );
masterFile>> payRate >> dependents >> eType;
numItems++;
}
The question is the "get" function does not work, it returns nothing. When this happens the rest of the read is messed up. Could someone tell me how to get this get to work. I must use the get function please do not suggest other methods.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Your code does not match the input file. There are TWO int values after the double, not just one !!
ASKER
Sorry, I made an error copying the text from the program. Please add
int eType; This is the second int field.
int eType; This is the second int field.
ASKER
Also the char field at the end of the file is to ignored.
I had something similar happen in my own code a while back and what was happening was that the get returned a whitespace character left in the file from reading in the thing before. Try doing two gets and discarding the first one.
>> int eType; This is the second int field.
Then you're missing the final character in the line ... (the gender if I'm not mistaken ... M/F). You need to read that too !
Then you're missing the final character in the line ... (the gender if I'm not mistaken ... M/F). You need to read that too !
May I ask why you want to delete this question ? Was your problem resolved ? If not, do you need further assistance ?
Post http:#20392825 tells how to solve these kinds of problems in general.
Post http:#20394370 points out a specific problem with the way input is processed.
I'd love nothing better than to help luling1972 further to make sure that his/her problem is resolved, but I can't do that without replies :)
Post http:#20394370 points out a specific problem with the way input is processed.
I'd love nothing better than to help luling1972 further to make sure that his/her problem is resolved, but I can't do that without replies :)
ASKER
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