LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices



Reply
 
Search this Thread
Old 04-07-2007, 02:08 PM   #1
Four
Member
 
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".

Thanks
 
Old 04-07-2007, 04:56 PM   #2
wjevans_7d1@yahoo.co
Member
 
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:

Code:
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, 05:52 PM   #3
Four
Member
 
Registered: Aug 2005
Posts: 298

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


Reply


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-04-2006 12:08 AM
fopen: No such file or directory CatBreath Linux - Software 4 03-27-2005 07:33 AM
how to locate file in home directory learnfast Linux - Newbie 3 03-14-2005 08:15 AM
.xession-errors file in my home directory drxsmurf Linux - Software 3 05-06-2004 10:01 PM
.cshrc file not in home directory on RH8 or RH9? aaron268 Linux - Newbie 4 05-30-2003 10:01 AM


All times are GMT -5. The time now is 08:24 AM.

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