1

my code is like this:

int line_number = 0; while (getline(infile, line)) { stringstream lineStream(line); line_number++; string token; while (lineStream >> token); } 

If some tokens are not what I expect, then I will output a syntax error with line number and character offset. But the question is how I'm able to get character offset, is there any simple way I can get tokens' first letter's position?

5
  • It might be possible to use tellg() to look at the updated position in the stringstream() after extracting a token, then subtracting the token's length. That feels iffy. In this kind of a situation, I would just parse each line manually, instead of using stringstream, and get down to the nitty-gritty details myself. Commented Oct 2, 2016 at 2:32
  • you mean, check character by character in each line? Commented Oct 2, 2016 at 2:38
  • Correct. A simple parsing loop. Commented Oct 2, 2016 at 2:45
  • That's going to give you the absolute stream position, you'd have to be careful to ensure you tracked the start-of-line tellg position: ideone.com/J1pjPL Commented Oct 2, 2016 at 2:53
  • tellg() is only useful for binary files... Also, your question has an answer here. Commented Oct 2, 2016 at 3:35

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.