plain C: opening a directory with fopen()

前端 未结 2 2057
南笙
南笙 2020-11-30 13:59

I have a program which opens a file and checks its length.

FILE* fd = fopen(argv[1], \"rb\");
fseek(fd, 0, SEEK_END);
size_t flen = ftell(fd);
if (flen == ((         


        
2条回答
  •  暖寄归人
    2020-11-30 14:39

    From http://pubs.opengroup.org/onlinepubs/7908799/xsh/fopen.html:

    The fopen() function will fail if: 
    [EISDIR] The named file is a directory and mode requires write access.
    

    At least on Linux, if you try to fopen("dirname", "wb") you get an EISDIR error. I also tried with a directory with d-------- access rights, and I still get EISDIR (and not EACCES.)

提交回复
热议问题