-3

I want to read integers in a line from a file.

For example the line is : 3/2+5-5

I think I need to use >>, but it stopped because of the characters;

I also try to use other functions, but they are all for characters.

Jutta
  • 513
  • 5
  • 13
  • 1
    There is no easy way to do this. I suggest reading in the whole string and then to tokenize it. – Fang Apr 27 '16 at 08:24

1 Answers1

2

As the @Fang already pointed out, there's no easy way to do it. You can read the whole line and tokenize it via the following code:

std::ifstream f("file.txt");

std::string line;
std::getline(f, line);

std::vector<std::string> integers;
boost::split(integers, line, boost::algorithm::is_any_of("+-*/"), boost::token_compress_on);

// Then convert strings from the integers container to ints
FrozenHeart
  • 19,844
  • 33
  • 126
  • 242