Take the 2-minute tour ×

I've been searching for a while to find this answer but I can't find a solution. Basically I'm trying to read and write to files in the c drive of my computer. However because it is windows 7 it chooses to be a pain in the back side and not give permission to my C program to do it. I have tried running it in administrator mode as well as trying to use different directories. When the following code is run:

FILE *fp = fopen(strIn, "r");
if(fopen(strIn, "r") == NULL)
    printf("Error: %d (%s)\n", errno, strerror(errno));

The returning error is "Error 13 permission is denied". I know that Microsoft don't really like using fopen any more as it favours fopen_s but I find difficulty in understanding how to implement it and there is a lack of resources on it as well. Is there any way around this problem as it is really setting me back at the moment. Much appreciated,


share|improve this question
What paths have you tried with? Unicode paths have been an issue with fopen in windows before. –  stefan Jul 6 '12 at 7:54
Well it being inputed as single slashed by the user. For example "C:\dtm100m\" then put in to a char array. Is this what you mean/ –  Jackter Jul 6 '12 at 8:04
not really. Please list some examples of failed strIn. –  stefan Jul 6 '12 at 8:13
while((ent = readdir(dir)) != NULL) //loop through each file in the directory. { //open the file and loop through each line converting it then outputing it into a new file sprintf(strIn,"%s%s",pathStr,ent->d_name); –  Jackter Jul 6 '12 at 8:16
print the strIn and paste the output log here in a comment or in your question. –  stefan Jul 6 '12 at 8:17

1 Answer 1

Your code is opening the file twice:

if(fopen(strIn, "r") == NULL)  /* Second open, leaks FILE *! */

This might cause errors, since Windows can be picky about allowing a file to be opened multiple times. The second line should read:

if(fp == NULL)
share|improve this answer
If I recall correctly the Windows implementation of fopen() opens the file in exclusive mode. –  Luke Jul 6 '12 at 13:54

Your Answer


