1

I am fairly new to C programming and trying to properly understand the ins and outs of memory management in C.

I made a simple program that would compile without any issues but whilst debugging gives me a segmentation error after the line printf("The next line gives a segmentation error");

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>      // Here you have the isdigit macro

int main()
{
    FILE *filePtr; 
    char fileStr[150];      // Buffer for reading file.

    filePtr = fopen("ManyRandomNumbersLog.txt", "r");
    
    printf("\n\nNow reading the file:\n");

    while (!feof(filePtr))
    {
        printf("The next line is a segmentation fault!\n");
        // WHYYYY?!?!?!?
        fgets(fileStr, 150, filePtr);
        printf("%s\n", fileStr);
    }

    return 0;
}

The fgets function call seems to be giving this error since the pointer has the following "error?" inside it:

file pointer error

Do you know what is the problem and how to prevent it?

I tried debugging it but could not figure out why the pointer cannot access that memory.

chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Always check if the file was opened before using the pointer in any way. `if (filePtr) { ... }` and read [Why is “while( !feof(file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Retired Ninja Aug 12 '23 at 13:56

2 Answers2

3

Always check if the file has been opened successfully. Also feof does not work the way you think.

int main()
{
    FILE* filePtr; 
    char fileStr[150];      // Buffer for reading file.

    filePtr = fopen("ManyRandomNumbersLog.txt","r");
    
    if(filePtr)
    {
        printf("\n\nNow reading the file:\n");
        while(fgets(fileStr, 150, filePtr))
        {
            printf("%s\n",fileStr);  
            //filestr will also contain '\n' at the end if it was present in the file.
        }
        fclose(filePtr);
    }

    return 0;
}

PS do not look inside the FILE structure as it will not give you nay valuable information.

0___________
  • 60,014
  • 4
  • 34
  • 74
3

You should always check for a potential failure of fopen() and report it with meaningful information:

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

int main()
{
    FILE *filePtr = fopen("ManyRandomNumbersLog.txt", "r");
    if (filePtr == NULL) {
        fprintf(stderr, "cannot open file %s: %s\n", 
                "ManyRandomNumbersLog.txt", strerror(errno));
        return 1;
    }
    
    printf("\n\nNow reading the file:\n");

    char fileStr[150];      // Buffer for reading file.

    // Do not use feof() for this loop:
    //    just try and read a line and stop upon failure to do so.
    while (fgets(fileStr, 150, filePtr)) {
        printf("%s\n", fileStr);
    }
    fclose(filePtr);

    return 0;
}
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Strangely enough, [`fopen()` is not required to set `errno` on failure](https://port70.net/~nsz/c/c11/n1570.html#7.21.5.3p9). IME most implementations do set it, though. And that does mean `fopen()` is free to munge `errno` when it *doesn't* fail... – Andrew Henle Aug 12 '23 at 15:09
  • @AndrewHenle: I guess it is a quality of implementation issue. – chqrlie Aug 12 '23 at 17:53