user1745860
user1745860

Reputation: 247

Delimiter extracting out a range of data

From textfile(Range.txt),

Range:1:2:3:4:5:6....:n:

There's a list of result, but i only need to extract digit 1 2 3 to the last digit, but sometimes the last digit may varies

so i read in the file, extract out the delimiter and push it into a vector.

ifstream myfile;
myfile.open("Range.txt");

istringstream range(temp);
getline(range, line,':');

test.push_back(line);

How do i capture all the value? I have this and it only capture one digit.

Upvotes: 1

Views: 95

Answers (2)

P0W
P0W

Reputation: 47784

I have this and it only capture one digit

You need to use a loop :-

while (getline(range, line, ':')) 
  test.push_back(line);

Later using the vector you can process it to get the integers only.

Upvotes: 2

vdenotaris
vdenotaris

Reputation: 13637

Plase, read that: Parsing and adding string to vector.

You just have to change the delimiter (from whitespace to :).

std::ifstream infile(filename.c_str());
std::string line;

if (infile.is_open())
{
    std::cout << "Well done! File opened successfully." << std::endl;
    while (std::getline(infile, line, ':'))
    {
        std::istringstream iss(line);
        std::vector<std::string> tokens{std::istream_iterator<std::string>(iss),std::istream_iterator<std::string>()};

        // Now, tokens vector stores all data.
        // There is an item for each value read from the current line.
    }
}

Upvotes: 0

Related Questions