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 ...
- 11-15-2008 #1Just 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?
- 11-15-2008 #2
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.


Reply With Quote
