11

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?

Alan Birtles
  • 32,622
  • 4
  • 31
  • 60
John
  • 794
  • 2
  • 18
  • 34
  • What do the docs say about Copenhagen failure types? There should be some way to find out. – xaxxon Dec 26 '11 at 07:56
  • Does the file exist (in the execution path)? – Sangeeth Saravanaraj Dec 26 '11 at 07:58
  • Check the file and/or containing directory's permissions. – prongs Dec 26 '11 at 07:59
  • 1
    Using `std::cout` means you're not writing pure C (that's C++). You should probably be using `std::cerr`. The most likely cause of trouble seems to me to be that the current directory is not the debug directory of your project - so the file isn't in the current directory. The error number should confirm that the file does not exist. – Jonathan Leffler Dec 26 '11 at 07:59

4 Answers4

12
#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

Jeegar Patel
  • 26,264
  • 51
  • 149
  • 222
  • the problem was not in opening the file. 'if(rd)' was evaluated to true and so the message line was executed. i should have used 'if(rd=NULL)' – John Dec 26 '11 at 08:12
  • if fopen() gets fail then it will return NULL so rd will be NULL now in your code you have written if(!rd) [not if(rd)] means it will be executed when rd will be NULL, if file open succesfully then that message will never prints.. and if you want to use == then you shoud write if(rd==NULL) noy if(rd=NULL) – Jeegar Patel Dec 26 '11 at 08:17
  • please always take care for what you are typing in your code and also on SO – Jeegar Patel Dec 26 '11 at 08:32
2

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.

Francis Upton IV
  • 19,322
  • 3
  • 53
  • 57
2

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

Sangeeth Saravanaraj
  • 16,027
  • 21
  • 69
  • 98
0

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.

dicaprio
  • 713
  • 2
  • 8
  • 25
  • i am 100% certain(as far as I know) I have the file in the working directory. Plus I have tried to open it without the binary mode and the result is the same. – John Dec 26 '11 at 07:59