Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 04-07-2007, 01:08 PM   #1
Registered: Aug 2005
Posts: 298

Rep: Reputation: 30
fopen a file in home directory

in the language C/C++
When I try to open a file in the home directory like so

fp =fopen("~/.aexec", "r");

fp is set to 0. How may I open this file? it exists and I could open it using vim "~/.aexec".

Old 04-07-2007, 03:56 PM   #2
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 30
Get the environment variable HOME. That will be what you should use instead of ~.

Then allocate an array of characters large enough to hold the ultimate full path. This will be the length of the HOME environment variable, 7 bytes for the /.aexec, and one more byte for the terminating NUL. Then combine the two strings. If all this is in a loop of any sort, free the allocated string when you're done with it.

For more help:

man getenv
man strlen
man malloc
man strcpy
man strcat
man free
If vim does this for you while you're inside the editor itself, then vim is recognizing the ~ and doing something equivalent to this. If you're specifying the file name on the shell command line, it's the shell that's making the same substitution.

Hope this helps.
Old 04-07-2007, 04:52 PM   #3
Registered: Aug 2005
Posts: 298

Original Poster
Rep: Reputation: 30
I got it to work thank you


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to protect a file/directory in your home login directory (RH Linux) jitsenho Linux - Security 9 07-03-2006 11:08 PM
fopen: No such file or directory CatBreath Linux - Software 4 03-27-2005 06:33 AM
how to locate file in home directory learnfast Linux - Newbie 3 03-14-2005 07:15 AM
.xession-errors file in my home directory drxsmurf Linux - Software 3 05-06-2004 09:01 PM
.cshrc file not in home directory on RH8 or RH9? aaron268 Linux - Newbie 4 05-30-2003 09:01 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:38 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration