fopen() quiz for my fellow experts.
Posted on 2004-10-13
I came across a less-than-obvious error the other day that I thought I'd share as a mini-puzzle.
A friend sent me some code (a C program). After compiling it I tried to run it and found that the application was getting an error. After stepping through the code, I discovered that the error lies here:
/* function -- abbreviated here because it's quite long */
fcb = fopen(str, "r");
if (fcb == NULL)
getcwd (cwd, 80);
printf("Failed to open %s (%d). cwd=%s\n", str, errno, cwd);
perror (" Open failure");
I've run it several times, with str containing strings such as "TEST" and "C:\TEST". Yet fopen () fails every time (returns NULL). The files named in str DO exists.
The error messages resemble:
Failed to open TEST (0). cwd=C:\DS\TH
Failed to open C:\DS\TH\TEST (0). cwd=C:\DS\TH
The question is, why does fopen() always fail?
And to head off the obvious:
The file does exist.
The user/program is allowed read access to the file.
The file is not already open.
The system is Windows/XP. (Meant as information, not an explanation!)
Let's see how good you guys really are. :)