what is the reason for fopen's failure to open a file

前端 未结 4 1854
醉梦人生
醉梦人生 2020-12-31 17:04

I have the following code where I am trying to open a text file.

char frd[32]=\"word-list.txt\";
   FILE *rd=fopen(frd,\"rb\");
   if(!rd)
       std::cout&         


        
相关标签:
4条回答
  • 2020-12-31 17:18

    Look at the errno variable which is set in the event of an error. It's a global variable. It's been a while, but probably include errno.h which will give you the definition.

    0 讨论(0)
  • 2020-12-31 17:20

    r Open for reading (existing file only) and rb Open for reading (existing file only) in binary mode. Make sure you have the file in your working directory.

    0 讨论(0)
  • 2020-12-31 17:31

    You can do man fopen - it says Upon successful completion fopen() return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error.

    Please check whether the file exists in the execution path or in your program, check the errno

    0 讨论(0)
  • 2020-12-31 17:34
    #include<stdio.h>
    #include <errno.h>
    
    int main()
    {
    errno = 0;
    FILE *fb = fopen("/home/jeegar/filename","r");
    if(fb==NULL)
        printf("its null");
    else
        printf("working");
    
    
    printf("Error %d \n", errno);
    
    
    }
    

    this way if fopen gets fail then it will set error number you can find those error number list at here http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html

    0 讨论(0)
提交回复
热议问题