Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

fopen

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 anyone tell me why it is not able to open the file?

like image 662
John Avatar asked Dec 26 '11 07:12

John


2 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);


}

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

like image 87
Jeegar Patel Avatar answered Oct 13 '22 00:10

Jeegar Patel


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.

like image 43
Francis Upton IV Avatar answered Oct 12 '22 22:10

Francis Upton IV