D-pag D-pag - 5 months ago 24
C++ Question

how to ignore/skip white spaces when reading from a textfile using string getline in c++

Hi I am trying to develop a program which can read from a text file for example

Edward Elric : 2000 300 3000 300
Super Trunks : 100 300 4000 900
Saitama Genos:
Goku Black: 12 333 33

I want the program to start reading the scores in each line but skip the blank spaces for example each record has a maximum of 4 scores however some records have less than 4 scores which are denoted by the whitespace i want the program to read these records skiping the white spaces i want this to be repeated until the end of file below is the code I have written for this part i am confused as to how i would proceed to do this any and all help is appreciated


I think you can use std::getline to greater advantage because it doesn't just get lines it will read up to a character you specify. For example the colon (:):

for(std::string line; std::getline(file, line);)
    // turn the line into a stream
    std::istringstream f(line);

    std::getline(f, line, ':'); // skip past ':'

    // read all the numbers one at a time
    for(int v; f >> v;)
        std::cout << v << ' ';

    std::cout << '\n';


2000 300 3000 300 

100 300 4000 900 

12 333 33