Right now, I have
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));
}
}
And When I do size = atoi(fileName) it returns 0. On multiple sites including this one, I’ve seen people do “atoi(argv[1])” but mine constantly returns 0. My sample.txt file has a bunch of 3-digit numbers separated by spaces. I am under the impression that once I get the size correctly, everything else below it will work.
atoidoes not tellssize, it just convertsstringintointeger.for knowing
sizeof file you need to seek to the end of the file and then ask for the position: