Input is Annoying for C

Back to Programming Languages forum

npmazzuca     2014-08-30 20:22:34

Because of the many new lines in the test data, I am unable to use the test data as is for input into a C program. Why am I unable to just use argc and argv, without changing the test data first by find/replacing '\n' with ' '? Is there a better way?

Rodion (admin)     2014-08-31 03:25:46
User avatar

I believe the most convenient way for C program is to use scanf function for most problems. It read data separated either with spaces or newlines. Something like this:

#include <stdio.h>
int main(void) {
    int n, a, b;
    scanf("%d", &n);
    while (n-- > 0) {
        scanf("%d %d", &a, &b);
        // do anything with a and b
    }
}

it will simply read from the input like this:

3
100 220
911 408
512 111

Hope this helps. With argc and argv the problem is that posix limit for it is usually as low as 4096 bytes.

Please login and solve 5 problems to be able to post at forum