user79292
user79292

Reputation: 815

getline in C++ - Help needed

I am using getline to read up to end of newline but c++ getline gets me stuff till space,

I have txt file data as

address(tab char)1420 Happy Lane

When I do

getline(reader, ss, '\t') I get address in ss string. when I do getline(reader, ss, '\n') I just get 1420.

I want full "1420 Happy Lane", How to get it ?

Thanks.

#include <fstream>
#include <string>
#include <iostream>
#include <vector>
#include <sstream>

using namespace std;
int main( int argc, char *argv[] )
{
        if( argc < 2 )
        {
                cout << "Missing filename as first argument" << "\n";
                exit(2);
        }

        vector<string> myvector;
        string ss;
        int i=0, j=0;

        ifstream reader(argv[1]);

        if (! reader )
        {
                cout << "Error opening input file : " << " " << argv[1] << '\n';
                return -1;
        }

        while( !reader.eof())
        {
                 if ((i+1) % 2 == 0 )
                        getline(reader, ss, '\n');
                 else
                         getline(reader, ss, '\t');
                if (ss[0] == '#')
                {
                        //Skip
                        getline(reader,ss, '\n');i=0;
                        continue;
                }

                i++;
                myvector.push_back(ss);
        }

        reader.close();

        vector<string>::iterator it;
        stringstream stream;
        int vecloc=1;
        string tag;
        string sData;

        cout << "myvector contains: \n";

        for ( it=myvector.begin() ; it < myvector.end(); it++ )
        {
                switch (vecloc)
                {
                case 1: stream << *it;  stream >> tag; vecloc++;break;
                case 2:
                                stream << *it;  stream >> sData;
                                // Do job
                                cout << tag << "   " << sData << "\n";
                                // Reset.
                                vecloc=1;       break;
                default :       break;
                }
                // Clear String stream
        stream.str(""); stream.clear();
        }

        return(0);
}

output

/home/sr/utl

cat abc.txt

hey c++ making me nuts.

/home/sr/utl

a.out abc.txt

myvector contains:

hey c++

Upvotes: 0

Views: 4185

Answers (3)

Karl Knechtel
Karl Knechtel

Reputation: 61498

You're trying to alternate between grabbing up until a \t and grabbing up until a \n. But the times that you find a '#' comment line throw off your alternation.

By far the easiest and most robust way to handle this sort of thing is to read each line first, and then re-parse the line.

Upvotes: 0

Michael Burr
Michael Burr

Reputation: 340178

Paste the actual code from your editor and double check that there isn't a newline (or maybe other unexpected non-printing characters) in your data file.

This works as expected here:

#include <iostream>
#include <sstream>

using namespace std;

int main()
{
    stringstream reader("address\t1420 Happy Lane\n");

    string ss;

    getline(reader, ss, '\t');
    cout << "1: " << ss << endl;

    getline(reader, ss, '\n');
    cout << "2: " << ss << endl;
}

Output:

1: address
2: 1420 Happy Lane

Upvotes: 3

Ruel
Ruel

Reputation: 15780

I got a split() function you can use for that. Use \t as the delimeter:

void split(std::string &string, std::vector<std::string> &tokens, const char &delim) {
    std::string ea;
    std::stringstream stream(string);
    while(getline(stream, ea, delim))
        tokens.push_back(ea);
}

Upvotes: 0

Related Questions