1

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);
            }
        }
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
evie
  • 73
  • 1
  • 8
  • 4
    The values are correct, except there should be 50 in the beginning. You’re reading characters, not numbers. If your file has the *text* `200` in it then that’s `50 48 48` in ASCII characters – Sami Kuhmonen Apr 18 '22 at 06:11
  • Aaaah, I had a feeling. Do you know how to read it in so I don't get the ascii values but the actual values from the text? – evie Apr 18 '22 at 06:14
  • 1
    it's being read fine, you're just printing it as `%d` rather than `%c`, so it's being treated as a `d`ecimal number rather than a `c`haracter – Mitchell Apr 18 '22 at 06:24
  • 1
    regarding: `int ch; if (inputFile == NULL) { perror("Could not open file"); } else { while (ch != EOF)` the first comparison of `ch` with `EOF` results in undefined behavior because the variable `ch` is not initialized – user3629249 Apr 19 '22 at 07:38

2 Answers2

2

You read a character with fgetc() but you want to read a number which you do with int d; fscanf(inputFile, "%d", &d).

Allan Wind
  • 23,068
  • 5
  • 28
  • 38
2

Your code has undefined behavior because you test while (ch != EOF) with ch uninitialized the first time. You should write:

    while ((ch = fgetc(inputFile)) != EOF) {
        [...]

Yet the problem is you read individual bytes instead of parsing the file contents for numbers expressed as decimal integers. You should use fscanf() to convert the text into integers.

You also forgot to close the file, causing resource leakage.

Here is a modified version:

#include <errno.h>
#include <stdio.h>
#include <string.h>

int readInputFile(const char *fileName, LinkedList *list)
{
    FILE *inputFile = fopen(fileName, "r");

    if (inputFile == NULL) {
        fprintf(stderr, "Could not open file %s: %s\n",
                fileName, strerror(errno));
        return -1;
    } else {
        int value, count = 0;
        char ch;
        while (fscanf(inputFile, "%d", &value) == 1) {
            printf("inserting %d\n", value);
            insertLast(list, value);
            count++;
        }
        if (fscanf(inputFile, " %c", &ch) == 1) {
            fprintf(stderr, "File has extra bytes\n");
        }
        fclose(inputFile);
        return count;  // return the number of integers inserted.
    }
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189