views:

370

answers:

8

I've been trying to open a file and output text, but I keep getting errors. So I thought I would start at the very beginning and just try opening the file. This is my code:

#include <stdio.h>
#include <stdlib.h>
#define CORRECT_PARAMETERS 3

int main(void)
{
    FILE *file;
    file = fopen("TestFile1.txt", "r");
    if (file == NULL) {
     printf("Error");
    }
    fclose(file);
}

When I run the file, "Error" gets printed to the console and that's it. The TestFile1.txt is in the same location as my .exe. Any thoughts? Thanks!

+2  A: 

Your executable's working directory is probably set to something other than the directory where it is saved. Check your IDE settings.

Mark Rushakoff
+3  A: 

How are you running the file? Is it from the command line or from an IDE? The directory that your executable is in is not necessarily your working directory.

Try using the full path name in the fopen and see if that fixes it. If so, then the problem is as described.

For example:

file = fopen("c:\\MyDirectory\\TestFile1.txt", "r");
file = fopen("/full/path/to/TestFile1.txt", "r");

Or open up a command window and navigate to the directory where your executable is, then run it manually.

As an aside, you can insert a simple (for Windows or Linux/UNIX/BSD/etc respectively):

system ("cd")
system("pwd")

before the fopen to show which directory you're actually in.

paxdiablo
No need for the backslashes, which often lead to subtle errors by people that have problems getting things like `fopen()` to work. You should recommend forward slashes.
Roger Pate
I neither recommend forward nor backward slashes since the standard doesn't mandate the format of the filename at all. But you're missing the point if you downvoted for that reason - the intent was just to demonstrate the use of a full file path.
paxdiablo
A: 

The output folder directory must have been configured to some other directory in IDE. Either you can change that or replace the filename with entire file path.

Hope this helps.

Richie
Great comments. Now I am able to open my file at least manually this way. How would I do it from the command line in Visual Studio? I tried this on XCode by adding parameters, but it just wasn't working out for me. I was hoping that Visual Studio would give me more luck. Thanks!
jet
XCode and Visual Studio might set different working directories, but the problem is the same, and so is the solution. You need to include code in your application that changes to the directory you want.
Wade Williams
+4  A: 

A little error checking goes a long way -- you can always test the value of errno or call perror() or strerror() to get more information about why the fopen() call failed.

Otherwise the suggestions about checking the path are probably correct... most likely you're not in the directory you think you are from the IDE and don't have the permissions you expect.

Rob Pelletier
+6  A: 

Instead of printf("Error");, you should try perror("Error") which may print the actual reason of failure (like permission problem, etc).

Kousik Nandy
If you find `perror` not flexible enough, you could also use `printf("Error: %d (%s)\n", errno, strerror(errno))` or even `printf("Error: %m\n")` (a Glibc extension).
ephemient
+3  A: 

Well, now you know there is a problem, the next step is to figure out what exactly the error is, what happens when you compile and run this?:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    FILE *file;
    file = fopen("TestFile1.txt", "r");
    if (file == NULL) {
      perror("Error");
    } else {
      fclose(file);
    }
}
Robert Gamble
A: 

In addition to the above, you might be intersted in seeing which directory is your current directory:

int MAX_PATH_LENGTH = 80;
char* path[MAX_PATH_LENGTH];
getcwd(path, MAX_PATH_LENGTH);
printf("Current Directory = %s", path);

This should work without issue on a gcc/glibc platform. (I'm most familiar with that type of platform) There was a question posted here that talked about getcwd & Visual Studio if you're on a Windows type platform.

Dan
A: 

Try using absolute path for the filename. And if you are using windows, use getlasterror() to see the actual error msg.

calvin