I am having trouble parsing the input file in the Auction Problem of Facebook HackerCup.
Every line of the input file contains 9 space-separated integers: N, P1, W1, M, K, A, B, C and D. According to the input constraints the numbers can be as big as 10^18. So, I decided to use long long int
to store the variables.
I am doing:
FILE *fr;
long long int N, P1, W1, M, K, A, B, C, D;
char line[1024];
fr = fopen ("input.txt", "rt");
while(fgets(line, 1024, fr) != NULL)
{
sscanf(line, "%lld %lld %lld %lld %lld %lld %lld %lld %lld", &N, &P1, &W1, &M, &K, &A, &B, &C, &D);
printf("N:%lld P1:%lld W1:%lld M:%lld K:%lld A:%lld B:%lld C:%lld D:%lld\n\n", N, P1, W1, M, K, A, B, C, D);
}
For a line 81834165 9999991 1 9999991 9999989 389999650 169999844 799999121 149999837
, I get N:81834165 P1:4367 W1:9999991 M:4078336 K:1 A:2292512 B:9999991 C:2292488 D:9999989
Can you please help me to point out the problem in this code? Any recommendation about parsing a similar file would also be appreciated.
Edit: I should add that it works for int
variables as in:
char line[1024];
int N, P1, W1, M, K, A, B, C, D;
strcpy(line, "81834165 9999991 1 9999991 9999989 389999650 169999844 799999121 149999837");
printf("Line: %s", line);
sscanf(line, "%d %d %d %d %d %d %d %d %d", &N, &P1, &W1, &M, &K, &A, &B, &C, &D);
printf("ret_val:%d\n\n", ret_val);
printf("N:%d P1:%d W1:%d M:%d K:%d A:%d B:%d C:%d D:%d\n\n", N, P1, W1, M, K, A, B, C, D);
I just noticed something strange. Printing more than one long long int
variables in one printf()
outputs wrong values while printing them in separate printf()
s outputs right values. I mean:
printf("N:%lld P1:%lld\n", N, P1);
printf("N:%lld\n", N);
printf("P1:%lld\n", P1);
outputs:
N:81834165 P1:2009288239
N:81834165
P1:9999991
Do you have any idea about it?
SOLUTION
I have found out that the problem was with the compiler I was using (gcc on MinGW)
On this compiler, I needed to replace %lld
with %I64d
. I found a similar problem at this post.