kingkonzgg
kingkonzgg

Reputation: 23

How to write a properly function in C for file pointer?

I am able to write the following main program:

int main(){
#include <stdio.h>
#include <stdlib.h>

FILE *fpointer = fopen("file.txt", "r+");

if (fpointer == NULL){
    printf("file does not exist!\n");
    return EXIT_FAILURE;

fclose(fpointer);
return 0;
}

However, I am not able to split this into a new function (open file and check for existince). I want to call the new function checkFileExistence(). This is how I tried to write it:

#include <stdio.h>
#include <stdlib.h>

int checkFileExistence(char* filepath){

        FILE* file=fopen(filepath,"w");

        if (file == NULL){
                printf("File does not exist!");
                return EXIT_FAILURE;
        }

        return EXIT_SUCCESS;
}

int main(){

        char* filepath = "test.txt";
        FILE* new_file_pointer=checkFileExistence(filepath);

        fclose(new_file_pointer);

        return 0;
}

But I keep getting errors for hours now. The error at this point is:

file.c:19:8: warning: incompatible integer to pointer conversion initializing 'FILE *'
      (aka 'struct __sFILE *') with an expression of type 'int' [-Wint-conversion]
        FILE* new_file_pointer=checkFileExistence(filepath);

How do I code correctly? I just want to use an own function to check the existence of a file without putting everything into the main function. Thank you in advance for the answer. I looked into the theory of data types, but still aren't able to use this correctly, because the pointer are somehow confusing me. Thank you very much. I love this platform and always appreciate all answers!

Upvotes: 0

Views: 108

Answers (2)

Luis Colorado
Luis Colorado

Reputation: 12708

Hmmmm.... if you #include <stdio.h> inside the body of main you'll probably run into trouble, as that limits the scope of all definitions made in stdio.h to the body of main. So you'll have to include it again if you need the stdio.h definitions in a different function you write after main. But this will not work, as stdio.h defines a flag that avoids it to be included again in the same compilation unit, to prevent double definition errors. So, never #include <stdio.h> inside a function or a block delimited by { and }.

Anyway, you have several problems in your second snippet of code:

#include <stdio.h>
#include <stdlib.h>

int checkFileExistence(char* filepath){

if you return int, you cannot assign it to a FILE * variable, as you have in your main routine. Better use FILE * checkFileExistence(char* filepath) as your prototype for the function.


        FILE* file=fopen(filepath,"w");

        if (file == NULL){
                printf("File does not exist!");
                return EXIT_FAILURE;

EXIT_FAILURE is an integer, but you want to return a pointer. It should be better to just exit(EXIT_FAILURE); so the program is finished (if you cannot open the file, what else you can do?) or return NULL, to indicate some trouble in the opening of the file to the calling code.

        }

        return EXIT_SUCCESS;

You should return the FILE * obtained from fopen, or you will have nothing to operate upon in main. Better write return file;.

}

int main(){

        char* filepath = "test.txt";
        FILE* new_file_pointer=checkFileExistence(filepath);

        fclose(new_file_pointer);

        return 0;
}

So your file, after editing, should be something like:

#include <stdio.h>
#include <stdlib.h>

FILE *checkFileExistence(char* filepath){

        FILE* file=fopen(filepath,"w");

        if (file == NULL){
                printf("File does not exist!");
                exit(EXIT_FAILURE);
                /* or return NULL; */
        }

        return file;
}

int main(){

        char* filepath = "test.txt";
        FILE* new_file_pointer=checkFileExistence(filepath);

        /* it should be convenient you say something, as you'll get the file
         * opened and closed without you seeing anything at the terminal. */
        if (new_file_pointer != NULL) {
            printf("File opened successfully\n");
        }

        fclose(new_file_pointer);

        return 0;
}

Upvotes: 1

Criss Hills
Criss Hills

Reputation: 189

Your return type from checkFileExistence() is int. But in main(), you are assinging it to a variable of datatype FILE.

Try changing

FILE* new_file_pointer=checkFileExistence(filepath);

to

int existance_status = checkFileExistence(filepath);

but this would cause a variable scope error, you might want to look out for that too!

Upvotes: 1

Related Questions