1

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.

user1745860
  • 207
  • 1
  • 5
  • 11

2 Answers2

2

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.

P0W
  • 46,614
  • 9
  • 72
  • 119
0

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.
    }
}
Community
  • 1
  • 1
vdenotaris
  • 13,297
  • 26
  • 81
  • 132