When I give the function the file's directory ( say /home/username/filename.txt ) I get the errors:
No such file or directory Segmentation fault (core dumped)
the part of getting the name works fine, but fopen()
returns NULL
My function's code is:
#define L_SIZE 128
FILE* openFile()
{
char dir[L_SIZE];
printf("Enter the file's directory: ");
if(fgets( dir, L_SIZE, stdin ))
{
printf("\nWe got the directory: %s\n",dir);
FILE* fp;
if ( (fp = fopen( dir, "r" )) == NULL )
{
perror("An error has occured");
return NULL;
}
else
{
return fp;
}
}
else
{
printf("Sorry, An error has occurs.\n");
return NULL;
}
}