What is the reason fopen fails to open a file

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<<"Coudn't open file\t"<<frd; 

I am using vc 2010 and the file is in the debug directory of this project. Can someone tell me why he cannot open the file?

+4
source share
4 answers
 #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); } 

so if fopen fails then it will set the error number, you can find here a list of error numbers http://pubs.opengroup.org/onlinepubs/009695399/functions/fopen.html

+6
source

Look at the errno variable, which is set in case of an error. This is a global variable. It has been a while, but probably includes errno.h, which will give you a definition.

+1
source

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 Upon successful completion fopen() return a FILE pointer. Otherwise, NULL is returned and errno is set to indicate the error .

Check if the file exists in the execution path or in your program, check errno

+1
source

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
source

Source: https://habr.com/ru/post/1387991/


All Articles