Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 566285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:57:39+00:00 2026-05-13T12:57:39+00:00

I am developing a portable C++ application. Development environment is Linux. I have a

  • 0

I am developing a portable C++ application. Development environment is Linux. I have a code that loads data from Xml file and create a object model out of it. Currently path to file is provided as /home/myuser/projectdir/xmlfilename.xml. This is problematic when I use from a different computer where the home directory name will be different. I tried something like ~/myuserprojectdir/xmlfilename.xml but it didn’t worked.

So is there a standard method in defining file names that will work on variety of platforms without any issues? Or any standard method that will work on Linux machines?

Any thoughts?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:57:40+00:00Added an answer on May 13, 2026 at 12:57 pm

    You need to locate the user’s home directory. To do this, Use getpwent to get the user record and from there the home directory. Then add the rest of the path to your xml file /myuserprojectdir/xmlfilename.xml to the value you get.

    This will work even if the users’s home directory is not /home/$USER. It works on linux and OSX, and will probably work on windows with cygwin installed.

    Here’s a working example with error checking omitted for clarity:

    #include <unistd.h>
    #include <sys/types.h>
    #include <pwd.h>
    
    main()
    {
      char* user = getlogin();
      struct passwd* userrecord;
      while((userrecord = getpwent()) != 0)
        if (0 == strcmp(user, userrecord->pw_name))
          printf("save file is %s/myuserprojectdir/xmlfilename.xml\n", userrecord->pw_dir);
    }
    

    output:

    save file is /Users/alex/myuserprojectdir/xmlfilename.xml
    

    This is how it works (from man getpwent):

    struct passwd * getpwent(void);
      // The getpwent() function sequentially reads the password database and is intended for programs that wish to
     process the complete list of users.
    
       struct passwd {
                   char    *pw_name;       /* user name */ // <<----- check this one
                   char    *pw_passwd;     /* encrypted password */
                   uid_t   pw_uid;         /* user uid */
                   gid_t   pw_gid;         /* user gid */
                   time_t  pw_change;      /* password change time */
                   char    *pw_class;      /* user access class */
                   char    *pw_gecos;      /* Honeywell login info */
                   char    *pw_dir;        /* home directory */ // <<----- read this one
                   char    *pw_shell;      /* default shell */
                   time_t  pw_expire;      /* account expiration */
                   int     pw_fields;      /* internal: fields filled in */
           };
    

    To get the username, use getlogin… here’s a snippet from man getlogin.

    char * getlogin(void);
      // The getlogin() routine returns the login name of the user associated with the current session ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 298k
  • Answers 298k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer "submit is not a function" means that you named your… May 13, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer Map the database fields to read only properties in your… May 13, 2026 at 7:33 pm
  • Editorial Team
    Editorial Team added an answer I think that GCC is pretty well documented so I'm… May 13, 2026 at 7:33 pm

Related Questions

I'm currently developing on a project written in Java. We have a bunch of
I have a GridView in a ListView contained in a ScrollViewer element. I understand
I'm developing a pretty portable indie game engine and also a demo game to
Hi I am developing desktop portable free application and I`m looking for portable database:
I am developing a Reporting Services solution for a DOD website. Frequently I'll have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.