Reputation: 57
I need to open this file, but it isn't opening and I dont know why:
#include<stdio.h>
void copy();
int main(void)
{
copy();
return 0;
}
void copy()
{
FILE *src = fopen("srcc.txt", "r+");
if(!src)
{
printf("It was not possible to open the file");
return;
}
}
It just pass the if condition and appear the message it was not possible to open the file
and the file is not created.
Upvotes: 0
Views: 113
Reputation: 4515
You could try using errno and strerror() to get a specific error code. For fopen() on most library implementations, the errno variable is also set to a system-specific error code on failure.
You could try something like:
#include <errno.h>
...
...
FILE *src = fopen("srcc.txt", "r+");
if(!src)
{
printf("ERROR: %d - %s\n", errno, strerror(errno)); // <---- This will print out some
// useful debug info for you
printf("It was not possible to open the file");
return;
}
errno.h
will have a list of defines for the common error codes and strerror()
will convert the errno
to a string that you can print out...
Likely codes you might see, in this case, include some of the following (just copied verbatim from errno.h - i left out the actual values...):
#define EPERM /* Operation not permitted */
#define ENOENT /* No such file or directory */
...
#define EACCES /* Permission denied */
...
Upvotes: 1
Reputation: 214
Chances are it can't find the file. I suggest making various copies of the file and putting them in various folders.
Upvotes: 0
Reputation: 16406
If the file exists, it's probably read-only ... you can't use "r+" on a file that isn't writable. Do you really need "r+" and not just "r"?
Upvotes: 0