Solved

find home directory c++

Posted on 2003-11-30
4
443 Views
Last Modified: 2011-08-18
i am using curl in my program and i need to be able to find the current users home directory, here is a snippet of the curl procedure

char *headerfilename = "logfile";
FILE *headerfile;
headerfile = fopen(headerfilename,"w");
curl_easy_setopt(curl_handle,   CURLOPT_WRITEHEADER ,headerfile);

i need that headerfilename to be /home/USERNAME/logfile

any help would be great

Thanks


 
0
Comment
Question by:nutterx
  • 3
4 Comments
 
LVL 24

Expert Comment

by:shivsa
ID: 9846640
u can get it from env
$HOME will be /home/USERNAME.
0
 
LVL 24

Accepted Solution

by:
shivsa earned 135 total points
ID: 9846801
#include <stdlib.h>

char *getenv(const char *NAME);
----------------------------------------------------

char *home = getenv( "HOME" );

char path[ MAXPATH];
strcpy( path, home );
strcat( path, "/your_directory/logfile" );
fopen( path, "w" );
0
 
LVL 24

Expert Comment

by:shivsa
ID: 9846808
or u can try this too.

char *name;
name = (char *)malloc(sizeof(char)*(PATH_MAX));
name = getcwd(name,PATH_MAX);

BTW: PATH_MAX is found in sys/types.h

also to leart more about getcwd use man getcwd.
0
 

Author Comment

by:nutterx
ID: 9846896
thanks shivsa
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question