fopen() returning a NULL pointer, but the file definitely exists

前端 未结 7 770
不思量自难忘°
不思量自难忘° 2020-12-31 02:25

The code I have is as follows:

FILE *txt_file = fopen(\"data.txt\", \"r\");
if (txt_file == NULL) {
    perror(\"Can\'t open file\");
} 

Th

7条回答
  •  囚心锁ツ
    2020-12-31 03:13

    Standard problem. Try

    FILE *txt_file = fopen("C:\\SomeFolder\\data.txt", "r");
    

    I.e. try opening it with the full absolute path first ; if it works then you just have to figure out what the current directory is with _getcwd() and then fix your relative path.

提交回复
热议问题