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

前端 未结 7 809
不思量自难忘°
不思量自难忘° 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 02:54

    I encountered the same errno to fopen on Linux from a script file corrupted by Windows.

    ENOENT 2 No such file or directory

    Wordpad on Windows (or some other Microsoft culprit) inserted CRLF = (0x0D, 0x0A) into my linux script files in place of newline = LF = 0x0A. When I read the file name into a buffer and called fopen if failed due to the invisible appended CR character.

    In the Codelite editor on Linux Mint I was able to show EOL characters (View > Display EOL) and remove them with find and replace, using copy and paste of the CRLF from the corrupted script files and the LF from an uncorrupted file into the text fields.

提交回复
热议问题