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);
            }
        }
    }
}
4
  • 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 Commented Apr 18, 2022 at 6: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? Commented Apr 18, 2022 at 6:14
  • 1
    it's being read fine, you're just printing it as %d rather than %c, so it's being treated as a decimal number rather than a character Commented Apr 18, 2022 at 6: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 Commented Apr 19, 2022 at 7:38

2 Answers 2

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.
    }
}
Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.