reading unknown number of integers from stdin (C)

c, integer, stdin

Solution

The way I would do it is to break it down into two operations: read a line, then read the integers in that line. Here is a lazy implementation using the standard C library:

char line[1024], *p, *e;
long v;
while (fgets(line, sizeof(line), stdin)) {
    p = line;
    for (p = line; ; p = e) {
        v = strtol(p, &e, 10);
        if (p == e)
            break;
        // process v here
    }
}

Problem

I need to read an input file like : ``` 1 19 20 41 23 2 41 52 43 3 90 91 941 4 512 5 6 51 61 ``` Each odd line is an integer. Each even line is unknown number of integers. It is very easy in C++ ``` while( cin >> k ){ ............ } ``` I'm not so used to C, so I couldnt make it in C. Any ways to do it?

Original source