Could not open file using fopen () function

I tried to open the file and output the text, but I get errors all the time. So I thought I would start from the beginning and just try to open 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" is printed to the console and that it is. TestFile1.txt is in the same place as my .exe. How to fix it?

+6
c file-io fopen stdio
source share
8 answers

Instead of printf("Error"); you should try perror("Error") , which can print the actual cause of the failure (for example, permission problem, invalid argument, etc.).

+20
source share

How do you run the file? Is it from the command line or from the IDE? The directory where your executable is located is not necessarily your working directory.

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

For example:

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

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

Aside, you can insert a simple one (for Windows or Linux / UNIX / BSD / etc respectively):

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

before fopen to show which directory you are in.

+10
source share

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

Otherwise, the suggestions for checking the path are probably correct ... Most likely, you are not in the directory that you think are from the IDE and do not have the expected permissions.

+5
source share

Your executable working directory is probably set to something other than the directory in which it is stored. Check your IDE settings.

+3
source share

So, now you know that there is a problem, the next step is to find out what exactly the error is, what happens when you compile and run it ?:

 #include <stdio.h> #include <stdlib.h> int main(void) { FILE *file; file = fopen("TestFile1.txt", "r"); if (file == NULL) { perror("Error"); } else { fclose(file); } } 
+3
source share

Try using an absolute path for the file name. And if you use Windows, use getlasterror () to see the actual error message.

+1
source share

The output folder directory must be configured to a different directory in the IDE. Either you can change this, or replace the file name with the entire path to the file.

Hope this helps.

0
source share

In addition to the above, you may be interested in displaying the 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 problems on the gcc / glibc platform. (I am most familiar with this type of platform). Here was a question that talked about getcwd and Visual Studio if you are on a platform like Windows.

0
source share

All Articles