0

I wrote this program by seeing a YouTube video. In that, he used fopen_s instead of fopen and I have the latest gcc and g++ compiler installed, but they don't support fopen_s. I am running into an error to close the file.

#include <stdio.h>

int main(){
    FILE* file;
    printf("loading file\n");
    file = fopen("testFile.txt", "r+");
    if (file == NULL){
        printf("ERROR!!!");
        fclose(file);
        return 1;
    }
    printf("file pointer is loded\n");
    fclose(file);
    return 0;
}

This is the output

loading file
Segmentation fault (core dumped)

What's going wrong?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 6
    If `file == NULL` don't call `fclose` since you didn't open anything. – Retired Ninja Oct 14 '20 at 03:21
  • 1
    By the way, I think fopen_s is a Microsoft special. It's not part of the standard and not available with gcc. – hookenz Oct 14 '20 at 03:23
  • @Matt: Actually, Microsoft certainly has `fopen_s()`, but it is also an optional part of C11 — see [Annex K.3.5.2.1 The `fopen_s` function](http://port70.net/~nsz/c/c11/n1570.html#K.3.5.2.1). I've not checked whether the MS and Annex K specifications are in sync — there are key differences for some of the interfaces in Annex K compared to Microsoft's implementation of the same name. It is not widely implemented, though. – Jonathan Leffler Oct 14 '20 at 04:04
  • Note that since the error message doesn't end with a newline, it doesn't appear before the code crashes. End messages with newlines in general. Reporting errors to standard error is also a good idea: `fprintf(stderr, "ERROR!!! Failed to open file %s for read/update\n", "testFile.txt");` (but this also illustrates why you should have a variable name rather than a string literal for the argument to `fopen()` (or `fopen_s()`) — you can use the variable in the error reporting without repeating the string literal. – Jonathan Leffler Oct 14 '20 at 04:06
  • You can see the Q&A [Do you use the TR 24731 'safe' functions?](https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions) for more information about problems with TR 24731 and Annex K compared with Microsoft. – Jonathan Leffler Oct 14 '20 at 04:11
  • As @JonathanLeffler states above, error messages *must* go to stderr. But you must also include the reason for the failure (eg, either with `perror`, or `strerror`, or some other mechanism.). For example: `if( ( file = fopen("testFile.txt", "r+")) == NULL ) { perror("testFile.txt"); ...` – William Pursell Oct 14 '20 at 16:11

1 Answers1

1

The problem is, as described in the comments by Retired Ninja, you shouldn't close the file in the case file = NULL because in that case nothing was opened. C will try to call the close function on a NULL pointer which will result in a seg fault.

To find out the reason, substitute your printf("ERROR!!"); for perror("error \n");, which will give you the reason for the file not being succesfully opened.

What I suspect, in your case, is that you should give the directory of the file when calling fopen.

Mölp
  • 142
  • 9