现在,我有
int main(int argc, char *argv[]) {
if (argc != 3) {
printf("Invalid number of command line parameters, exiting...\n");
exit(1);
}
int *numReadings;
load_readings(argv[1], numReadings);
return 0;
}
int *load_readings(char fileName[], int *numReadings) {
FILE *in = NULL;
in = fopen(fileName, "r");
if (in == NULL) {
printf("Unable to open a file named \"%s\", exiting...\n", fileName);
fclose(in);
exit(4);
}
printf("%s\n", fileName);
int size = atoi(fileName);
printf("Size is %d\n", size);
int *data = (int *) calloc(size, sizeof(int));
int i;
for (i = 0; i < size; i++)
fscanf(in, "%d", (data + i));
}
}
当我执行 size = atoi(fileName) 时,它返回 0。在包括这个站点在内的多个站点上,我看到人们执行“atoi(argv[1])”,但我的总是返回 0。我的 sample.txt 文件有一堆由空格分隔的 3 位数字组成。我的印象是,一旦我得到正确的尺寸,它下面的所有其他东西都会起作用。