Solved

C++ reading text file extracting c-string using get function

Posted on 2007-12-02
11
3,240 Views
Last Modified: 2008-06-29
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.
   
0
Comment
Question by:luling1972
  • 5
  • 3
11 Comments
 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 20392825
I assume that each field is on a separate line in the file ?

If so, the cin>> that reads the ID does not skip the end-of-line character, and thus the get will not read the name, but an empty string (whatever comes after the ID on the same line).

For input it's recommended to use getline to read a line from the file, and then parse that line to get the values out of it.

If you have to use get, then you can do something similar, or you'll have to make sure to skip to the next line before reading the name.
0
 

Author Comment

by:luling1972
ID: 20392883
All four  four fiedls are on the same line, Below is the file itself:

 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
 
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20392911
Your code does not match the input file. There are TWO int values after the double, not just one !!
0
 

Author Comment

by:luling1972
ID: 20393029
Sorry, I made an error copying the text from the program. Please add

int   eType;    This is the second int field.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:luling1972
ID: 20393040
Also the char field at the end of the file is to ignored.
0
 

Expert Comment

by:megaphreak
ID: 20393294
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.
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20394370
>> 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 !
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21843716
May I ask why you want to delete this question ? Was your problem resolved ? If not, do you need further assistance ?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 21877001
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 :)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Log files are useful in diagnosing and repairing problems.  This is a list of common log files and their standard locations that I've compiled.   While this is not exhaustive, it is a pretty good list that I've found to be useful.  I may update it f…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now