1

i am working in C and want to open a simple text file then perform some processing over it. My code looks like

FILE *pFile;
pFile = fopen("d:\\series.txt", "r");

if (pFile == NULL)
{
    printf("File not found or Unable to open file\nPress any key to continue . . ." );
        getch();
        return;
    }   
    else
    {
         //process here
    }

every time the condition becomes true if (pFile == NULL) so i am not able to perform processing on file.

I check that file exist in my drive with same name and its open properly when i double click on it

3
  • 4
    probably you don't have permissions to read it. Put it in d:\some_dir\ and try again. Commented Mar 30, 2011 at 10:28
  • capital D for a drive letter? Seems like you're on Windows? Commented Mar 30, 2011 at 10:38
  • 3
    @Henno: Windows does not require capital letters for drives. In fact, Windows filesystems are overwhelmingly case-insensitive in general. Commented Mar 30, 2011 at 10:38

1 Answer 1

8

Try putting this inside the if block:

perror(NULL);

That should give a descriptive error message, so you know what went wrong.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.