Using getline multiple times on one line of input

I've been stuck on this for hours, and have scoured the internet for any insight on this particular issue with no luck.

I'll simplify the problem here.

I have a lines in an input file:
1
2
3
John Smith          1234 Main St   New York  
Carol Thomas        5678 5th Ave   Columbus
...


and I am reading into character arrays using code like this:

1
2
3
infile.getline(name,20);
infile.getline(street,15);
infile.getline(city,10);


but when I print the output using something like this:
1
2
outfile << "Owner Name:   " << name << endl;
outfile << "Address:      " << street << city << endl;


(let's pretend I included spaces between the address components, which I omitted to save space)

I get an output file of:
1
2
Owner Name:     John Smith
Address:        


The output stops after the name variable, which I believe is stored correctly.
I don't think anything is storing in the address pieces, this is the problem.

I think it's because I'm not using getline() properly in this case. Can you string together multiple getline()s like that?

If this were my design, I'd read in strings instead, but that is not possible in this case.

Any help is greatly appreciated.
Check infile.fail() after each getline. Note that the fail flag will be set if the delimiter was not found.
http://www.cplusplus.com/reference/istream/istream/getline/

Also, is the data in each line of the file simply separated by multiple spaces, or is the tab character '\t' used between each field?
Last edited on
The data is separated by a number of spaces, making each line consistent in the length of each data piece.

ok, thanks. (In my opinion the tab delimiter could have made things simpler, but one has to work with things the way they are, not the way one might wish them to be).

I adjusted the field length to match what seems to be in the data file:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
    char name[21];
    char street[16];
    char city[11]; 

    while (infile)
    {
        infile.getline(name,21);
        infile.clear();
        
        infile.getline(street,16);
        infile.clear();
        
        infile.getline(city,11);
        if (infile)
        {
            cout << "Owner Name:   " << name << endl;
            cout << "Address:      " << street << city << endl;
        }
    }
Last edited on
Or, use get instead of getline when appropriate:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
    char name[21];
    char street[16];
    char city[11]; 

    while (infile)
    {
        infile.get(name,21);        
        infile.get(street,16);
        infile.getline(city,11);

        if (infile)
        {
            cout << "Owner Name:   " << name << endl;
            cout << "Address:      " << street << city << endl;
        }
    }



Thanks for your suggestions. I was initially thrown off by the character arrays, as my previous experiences have all used strings instead. This is also one of my first instances using getline, so this is all new to me.

Giving these a try and will report back.

Edit: Wow. Clearing infile fixed my problem (this problem at least).
It's unbelievable the amount of time I spent trying to fix this issue with other workarounds. Thanks for your input, and I will be noting the get bit as well, for the future.
Last edited on
If you have a free choice, I'd go for the get() rather than getline() option.

Ideally the fail flag should indicate whether or not the operation was successful. Using a method which will always set the fail flag even when it has retrieved the intended data would seem like poor design (even though I suggested it).
Topic archived. No new replies allowed.