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!