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

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

    Is it possible that the filename is not really "data.txt"?

    On Unix, filenames are really byte strings not character strings, and it is possible to create files with controls such as backspace in their names. I have seen cases in the past in which copy-pasting into terminals resulted in files with ordinary-looking names, but trying to open the filename that appears in a directory listing results in an error.

    One way to tell for sure that the filenames really are what you think they are:

    $ python
    >>> import os
    >>> os.listdir('.')
    

提交回复
热议问题