0

I get the above error in CppCheck but I can't see what's wrong.I guess the error is the reason my code doesn't find any files,even if they exist in my computer.Any help is appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 80

char *getchoice(void);
void getfile(char *filename);

int main() {
    char *choice; 

    choice=getchoice();
    getfile(choice);
    return 0;
}

char *getchoice(void) {
    char *filename;
    filename=malloc(SIZE);
    printf("Enter the name of the text file: ");
    scanf("%30s",filename);
    return filename;
}

void getfile(char *filename) {
    FILE *fp;
    fp=fopen(filename,"r");
    if (fp==NULL){
        printf("The entered file does not exist.");
        printf("\n");
    }
    else{
        printf("The file exists.");
    }
    fclose(fp);
    return;
}
Oka
  • 23,367
  • 6
  • 42
  • 53
Jim
  • 9
  • 2
  • 2
    Please make the title a short summary of your question or problem, and copy-paste (as text) and output relevant to the question/problem into the actual question body. And please take some time to read [the help pages](http://stackoverflow.com/help), take the SO [tour], read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). – Some programmer dude Apr 25 '22 at 12:06
  • 6
    The `fclose` should not be called if `fp` is NULL. – kaylum Apr 25 '22 at 12:07
  • Please [edit] and show the __verbatim__ error log – Jabberwocky Apr 25 '22 at 12:09
  • Also, if `fopen` fails it might be because other errors that the filename can't be found. On some systems (like Linux or macOS) the value of `errno` will be set to the actual error. Read the `fopen` manual page for your system to see how to find out the error. – Some programmer dude Apr 25 '22 at 12:09
  • Did you verify that the file name you type as input is properly passed to `getfile`? What do you input? Is it a relative or an absolute path? – Gerhardh Apr 25 '22 at 12:11
  • Side note: you never free the filename returned by `getchoice`. – Jabberwocky Apr 25 '22 at 12:11
  • BTW: CppCheck won't check if filenames exist or not, this is only done during runtime. – Jabberwocky Apr 25 '22 at 12:12

1 Answers1

1

Here is a list of things to do, in order to clean up this program:

  • Handle the event that malloc fails, so that scanf and getfile are not passed NULL.
  • Check that scanf successfully performed the expected number of conversions, to ensure filename contains valid data.
  • Use perror to give more accurate information about why malloc or fopen failed.
  • Avoid passing NULL to fclose, in the event that fopen failed.
  • free memory allocated by malloc (Unlike fclose, free may be safely passed NULL).
#include <stdio.h>
#include <stdlib.h>

char *getchoice(void);
void getfile(char *filename);

int main(void) {
    char *choice = getchoice();

    if (choice)
        getfile(choice);

    free(choice);
}

char *getchoice(void) {
    char *filename = malloc(80);

    if (filename) {
        printf("Enter the name of the text file: ");

        if (1 != scanf("%79s", filename)) {
            free(filename);
            return NULL;
        }
    } else {
        perror("malloc");
    }

    return filename;
}

void getfile(char *filename) {
    FILE *fp = fopen(filename, "r");

    if (fp) {
        puts("File opened.");
        fclose(fp);
    } else {
        perror("fopen");
    }
}
Oka
  • 23,367
  • 6
  • 42
  • 53