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?

4
  • What do the docs say about Copenhagen failure types? There should be some way to find out.
    – xaxxon
    Dec 26, 2011 at 7:56
  • Does the file exist (in the execution path)? Dec 26, 2011 at 7:58
  • Check the file and/or containing directory's permissions.
    – prongs
    Dec 26, 2011 at 7: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. Dec 26, 2011 at 7:59

4 Answers 4

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

3
  • 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, 2011 at 8: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) Dec 26, 2011 at 8:17
  • please always take care for what you are typing in your code and also on SO Dec 26, 2011 at 8: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.

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

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.

1
  • 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, 2011 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.