void readfromFile() {
FILE *myFile;
myFile = fopen("matrikel.txt", "r");
//read file into array
int numberArray[12];
int i;
if (myFile == NULL) {
printf("Error Reading File\n");
exit (0);
}
for (i = 0; i < 12; i++) {
fscanf(myFile, "%d,", &numberArray[i] );
}
for (i = 0; i < 1; i++) {
printf("Number is: %d\n\n", numberArray[i]);
}
fclose(myFile);
}
"matrikel.txt" contains
808098822790
The number seems to be too long for the int numberArray[12], when running the code it prints a random number.
When cutting some of the single integers from the end of the number it works, the maximum length seems to be 9.
I'm not quite sure but shouldn't the fscanf in the first for loop print one single digit number into each cell of numberArray[]?
"%d,"for reading the numbers, that expects each number to have a comma after it. Perhaps you want to read each digit separately instead? Then I suggest you read each digit as a character instead, and then convert that character to its correspondingintvalue instead.charvariable? Then convert it (by doing e.g.char_variable - '0') and store that value in theintarray you have?