Find the answer to your Linux question:
Results 1 to 2 of 2
Hello there FILE *fp; if ( ( fp = fopen( "$HOME/temp/file.txt", "r" ) ) == NULL ) $HOME/temp/file.txt wont work nor ~/temp/file.txt Is there a way to make it work ...
  1. #1
    Just Joined!
    Join Date
    Nov 2008
    Posts
    1

    fopen() absolute path

    Hello there

    FILE *fp;
    if ( ( fp = fopen( "$HOME/temp/file.txt", "r" ) ) == NULL )

    $HOME/temp/file.txt wont work nor ~/temp/file.txt

    Is there a way to make it work using an absolute path like this?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    You want to start with your home directory, yes? That's the HOME environment variable. You need getenv() for that. Once you have the value of HOME, copy it into your own array of characters using strcpy(). Then tack on the rest of the path using strcat(). Don't forget to make your array of characters large enough to hold this. To make sure it's large enough, use strlen() to conveniently count the number of characters in both the HOME variable and the string you're tacking onto the end. You may wish to use malloc() to allocate your array of characters. Don't forget that you'll need one more byte at the end of the combined string: the NUL character which indicates the end of a string.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...