I have a simple file reading algorithm but it's not returning the values that are in the file. The below is the code. The input txt values are 200 53 65 98 183 37 122 14 124 65 67 but the code is returning 48 48 32 53 51 32 54 53 32 57 56 32 49 56 51 32 51 55 32 49 50 50 32 49 52 32 49 50 52 32 54 53 32 54 55 -1 and I'm not sure why.
It should be taking the read in values and putting it into the linked list.
int readInputFile(char *fileName, LinkedList *list)
{
FILE *inputFile = fopen(fileName, "r");
int ch;
if (inputFile == NULL)
{
perror("Could not open file");
}
else
{
while (ch != EOF)
{
ch = fgetc(inputFile);
printf("%d", ch);
if (ferror(inputFile))
{
perror("Error reading from source file.");
}
else
{
//printf("%d", ch);
insertLast(list, ch);
}
}
}
}
200in it then that’s50 48 48in ASCII characters%drather than%c, so it's being treated as adecimal number rather than acharacterint ch; if (inputFile == NULL) { perror("Could not open file"); } else { while (ch != EOF)the first comparison ofchwithEOFresults in undefined behavior because the variablechis not initialized