B
Bernard Liang
Consider the following excerpt below, with the included relevant
declarations.
Firstly, the lines marked with **, ***, **** at the beginning are not
supposed to have those stars at the beginning; they are only used to
direct attention.
Suppose, first of all, that the lines with the **** are not present.
p_num is somehow getting changed from its previous value to 0 by the
sscanf command (***) that doesn't even involve p_num. I cannot
understand it, how I could possibly be doing anything that would modify
its value. The lines marked ** were added to determine that that was
indeed the line causing the problem. (the value was 1 before, and 0 after).
I then added the lines marked **** to remedy the problem, but being of
the philosophy that such "cheap fixes" are very poor programming style,
I wanted to see if anyone could shed some insight on this.
Code follows.
int main(int argc, char** argv) {
int pktsize, interval, numgums, p_pktsize, p_interval, p_num;
char trial, p_trial;
int wtf;
char buffer[200];
FILE* fp;
fp = fopen(argv[1], "r");
....
while (fgets(buffer, 160, fp) != NULL) {
**printf("how did p_numgums suddenly change to 0=%d?!?!\n", p_num);
****wtf = p_num;
*** if (sscanf(buffer, "%d %d %d %1s %*d %lf", &pktsize,
&interval, &numgums, &trial, &commrate) == 5) {
****p_num = wtf;
**printf("ok, now what? %d\n", p_num);
printf("from the string |%s", buffer);
printf(" cur data set: pktsize=%d interval=%d num=%d trial=%c cmr=%f\n",
pktsize, interval, numgums, trial, commrate);
printf("prev data set: pktsize=%d interval=%d num=%d trial=%c cmr=%f\n",
p_pktsize, p_interval, p_num, p_trial, p_commrate);
if (count == -1) {
count = 1; ++j;
min = max = runtot = commrate;
} else if ((p_pktsize == pktsize) && (p_interval ==
interval) && (p_num == numgums)) {
++count; ++j;
runtot += commrate;
if (fcompare(commrate, min) < 0) min = commrate;
if (fcompare(max, commrate) < 0) max = commrate;
} else {
printf("%d\t%d\t%d\t%c\t%d\t%f\t%f\t%f\n",
p_pktsize, p_interval, p_num, p_trial, count,
runtot / count, max, min);
count = 1; j = 1;
min = max = runtot = commrate;
}
p_pktsize = pktsize;
p_interval = interval;
printf("numgums was changed from %d", p_num);
p_num = numgums;
printf("to %d\n", p_num);
p_trial = trial;
p_commrate = commrate;
}
}
return 0;
}
The head (-n10) of the file I am feeding it via argv[1] looks likes this:
----
1000 10 1 a 10 91.041515 1500
1000 10 1 b 11 91.036757 1491
1000 10 1 c 12 87.535014 1500
1000 10 3 a 10 51.517332 859
1000 10 3 a 11 54.095827 910
1000 10 3 a 12 52.226982 890
1000 10 3 b 20 37.465007 629
1000 10 3 b 21 47.486709 786
1000 10 3 b 22 49.602106 829
1000 10 3 c 30 36.402570 629
declarations.
Firstly, the lines marked with **, ***, **** at the beginning are not
supposed to have those stars at the beginning; they are only used to
direct attention.
Suppose, first of all, that the lines with the **** are not present.
p_num is somehow getting changed from its previous value to 0 by the
sscanf command (***) that doesn't even involve p_num. I cannot
understand it, how I could possibly be doing anything that would modify
its value. The lines marked ** were added to determine that that was
indeed the line causing the problem. (the value was 1 before, and 0 after).
I then added the lines marked **** to remedy the problem, but being of
the philosophy that such "cheap fixes" are very poor programming style,
I wanted to see if anyone could shed some insight on this.
Code follows.
int main(int argc, char** argv) {
int pktsize, interval, numgums, p_pktsize, p_interval, p_num;
char trial, p_trial;
int wtf;
char buffer[200];
FILE* fp;
fp = fopen(argv[1], "r");
....
while (fgets(buffer, 160, fp) != NULL) {
**printf("how did p_numgums suddenly change to 0=%d?!?!\n", p_num);
****wtf = p_num;
*** if (sscanf(buffer, "%d %d %d %1s %*d %lf", &pktsize,
&interval, &numgums, &trial, &commrate) == 5) {
****p_num = wtf;
**printf("ok, now what? %d\n", p_num);
printf("from the string |%s", buffer);
printf(" cur data set: pktsize=%d interval=%d num=%d trial=%c cmr=%f\n",
pktsize, interval, numgums, trial, commrate);
printf("prev data set: pktsize=%d interval=%d num=%d trial=%c cmr=%f\n",
p_pktsize, p_interval, p_num, p_trial, p_commrate);
if (count == -1) {
count = 1; ++j;
min = max = runtot = commrate;
} else if ((p_pktsize == pktsize) && (p_interval ==
interval) && (p_num == numgums)) {
++count; ++j;
runtot += commrate;
if (fcompare(commrate, min) < 0) min = commrate;
if (fcompare(max, commrate) < 0) max = commrate;
} else {
printf("%d\t%d\t%d\t%c\t%d\t%f\t%f\t%f\n",
p_pktsize, p_interval, p_num, p_trial, count,
runtot / count, max, min);
count = 1; j = 1;
min = max = runtot = commrate;
}
p_pktsize = pktsize;
p_interval = interval;
printf("numgums was changed from %d", p_num);
p_num = numgums;
printf("to %d\n", p_num);
p_trial = trial;
p_commrate = commrate;
}
}
return 0;
}
The head (-n10) of the file I am feeding it via argv[1] looks likes this:
----
1000 10 1 a 10 91.041515 1500
1000 10 1 b 11 91.036757 1491
1000 10 1 c 12 87.535014 1500
1000 10 3 a 10 51.517332 859
1000 10 3 a 11 54.095827 910
1000 10 3 a 12 52.226982 890
1000 10 3 b 20 37.465007 629
1000 10 3 b 21 47.486709 786
1000 10 3 b 22 49.602106 829
1000 10 3 c 30 36.402570 629