2

I am running a simple program to open a file. Some details about the OS and processor:

 .Ubuntu 64bits
 .Processor AMD Ryzen 7

Everytime when i try to execute always receive this error: No such file or directory Already installed sudo apt-get install lib32z1 but the problem remains.Here is the code of the C program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <errno.h>
    #include <fcntl.h>
> 

void main(void)
{

    char* filename = "~/Documents/Manipulation/addresses.txt";
    
    printf("\nUsing fopen fuction");
    
    FILE* arch = fopen(filename, "r");

    if(arch == 0)
    {
      perror("fopen");
      printf("\n arch is null");
      exit(0);
    }
    else
    {
        printf("\n Arch is opened..");
    }
    
}

One important detail is when I use the function open("addresses", O_RDONLY) it works well.See the code below:

int fd;

printf("\nUsing open fuction");


fd = open ("addresses", O_RDONLY);
if(fd == 0)
{
    fprintf(stderr, "can't open %s: %s\n", filename, strerror(errno));
    exit(1);

}
else
{
    printf("\n Arch is opened..");
}

Does someone know how to fix this?

Cristik
  • 30,989
  • 25
  • 91
  • 127
  • 4
    You are using different file paths in your two examples. Try again using the same path. – MikeCAT Feb 26 '23 at 02:27
  • 5
    `fopen()` doesn't do shell level things like expanding a leading tilde to the users home directory. – Shawn Feb 26 '23 at 02:45
  • 1
    Pass the filename on the command line and change the `main` signature so that it can interrogate command line arguments. Let the shell do the heavy lifting. – Jeff Holt Feb 26 '23 at 03:21

1 Answers1

2

As noted in the comments above, the tilde character in the path literal will not be translated into the user's home directory. What you probably would need to do on your Linux system is retrieve the current user home directory information as noted in this previous solution User home directory.

Utilizing this solution, following is a refactored version of your code.

#include <unistd.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

int main(void)
{
    struct passwd *pw = getpwuid(getuid());

    const char *homedir = pw->pw_dir;

    char filename[200];

    strcpy(filename, homedir);
    strcat(filename, "/Documents/Manipulation/addresses.txt");

    printf("\nUsing fopen fuction");

    FILE* arch = fopen(filename, "r");

    if(arch == 0)
    {
        perror("fopen");
        printf("\n arch is null");
        exit(0);
    }
    else
    {
        printf("\n Arch is opened..");
    }
    return 0;
}

Note the addition of the "unistd.h" and "pwd.h" include files. With that bit of refactoring, following is a test execution of the program.

@Vera:~/C_Programs/Console/LinuxFile/bin/Release$ ./LinuxFile 

Using fopen fuction
 Arch is opened..

Give that a try to see if it meets the spirit of your project.

NoDakker
  • 3,390
  • 1
  • 10
  • 11
  • Man, thank you so much!!!! It worked perfectly. Just to add one more thing if somebody needs to use the fopen() function: beyond the code that you gave me, i passed the path without the file extension>> "/Documents/Manipulation/addresses"), when i use the .txt extension, the fopen() continues showing "No such file or directory ". – César Espíndola Feb 26 '23 at 13:30