Search code examples
clinefgetsscanf

C: how to store integers by reading line by line with a random layout?


I need to read a file and store each number (int) in a variable, when it sees \n or a "-" (the minus sign means that it should store the numbers from 1 to 5 (1-5)) it needs to store it into the next variable. How should I proceed?

I was thinking of using fgets() but I can't find a way to do what I want.

The input looks like this:

0
0
5 10
4
2 4
5-10 2 3 4 6 7-9
4 3

These are x y positions.


Solution

  • I'd use fscanf to read one int at a time, and when it's negative, it is obviously the second part of a range. Or is -4--2 a valid input?