0

Thanks for all answers, now i have a new question: why whatever i input the output is always GREAT? Like if the input is not pokemon.csv, then it should returns me "pokemon.csv file is not found. Please enter the name of the file again or enter 1 to exit" instead.
Is there a issue with my fopen format or logic?

int pexists(); int main(){ FILE *pokemon; int rc = 0; char file_name[30]; printf("Please enter the NAME of the file that containing the Pokemon descriptions:\n"); scanf("%c", file_name); rc = pexists(); return 0; } /*********************************************************************/ //function that do the exist checking. int pexists(){ FILE *pokemon; //int rc = 0; // open the file by reading. pokemon = fopen("file_name", "r"); if(!pokemon){ printf("Pokemon file is not found. Please enter the name of the file again or enter 1 to exit.\n"); exit(1); } else{ printf("GREAT\n"); } fclose(pokemon); } 
5
  • Did you intend to open the file written into the terminal? Your code always opens "pokemon.CSV". Should it instead fopen file_name? Commented Jul 24, 2021 at 6:41
  • also put fclose(pokemon) above return 0;, otherwise its not gonna be reached Commented Jul 24, 2021 at 8:14
  • Does this answer your question? What's the best way to check if a file exists in C? and Checking a file exists in C and Fastest way to check if a file exist using standard C++/C++11/C? Commented Jul 24, 2021 at 8:56
  • Two changes are required. (1) replace "pokemon.csv" for file_name inside the fopen and (2) move the fclose(pokemon) inside the else{} to avoid segmentation fault. Moreover be aware that there is a limitation in file name's length. Commented Jul 24, 2021 at 15:31
  • Thanks for all answers, would anyone please help me out with my new question? Commented Jul 25, 2021 at 21:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.