LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-31-2008, 05:44 PM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
c++ - getting home path for ifstream?


i have this:
Code:
int main()
{
   ifstream infile("/home/user_acct/path/to/file/to/read.txt");

   if (!infile)
   {
      cerr << "Could not open file!"  <<  endl;

      return 1;
   }
}
i want to replace '/home/user_acct' with a variable so i can use
the code on more than one box but am getting errors. this is
something i've tried:
Code:
int main()
{
   char homepath = getenv("HOME");

   ifstream infile(homepath + "/path/to/file/to/read.txt");

   if (!infile)
   {
      cerr << "Could not open file!"  <<  endl;

      return 1;
   }
}
could someone show me how to do what i'm trying to do here?

thanks,
BabaG
 
Old 05-31-2008, 05:57 PM   #2
AceofSpades19
Senior Member
 
Registered: Feb 2007
Location: Chilliwack,BC.Canada
Distribution: Slackware64 -current
Posts: 2,079

Rep: Reputation: 58
use strcat() or strncat() and use them to add the $HOME path on to the other path and then use ifstream to open the resulting variable
 
Old 05-31-2008, 06:05 PM   #3
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 78
A few things:
  1. getenv returns a char * not a char.
  2. Adding two char * variables does not concatenate them.
  3. If you want to do the plain C way, you should have a buffer to hold the resulting filename and use strncat() on that.
  4. Alternatively you can use realpath() with the ~ character as the home path.
  5. If you want to go the C++ way, you can use std::string (which when added to a char* does what is expected). For example,
    Code:
    int main()
    {
       std::string homepath = getenv("HOME");
    
       ifstream infile((homepath + "/path/to/file/to/read.txt").c_str());
    
       if (!infile)
       {
          cerr << "Could not open file!"  <<  endl;
    
          return 1;
       }
    }
 
Old 06-01-2008, 04:30 PM   #4
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
perfect! thanks osor and AceOfSpades19.

BabaG
 
  


Reply



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
java home/path konfigure SUSE / openSUSE 4 07-02-2007 09:44 PM
Path Problem with /home Diomedes Linux - Software 1 02-12-2007 11:26 PM
can't create symlink to my home path nick623 Linux - General 13 02-13-2006 09:36 PM
Changing home dir path? NonSumPisces Linux - Newbie 4 08-22-2004 04:16 PM
adding a $PATH to your /home emetib LinuxQuestions.org Member Success Stories 0 04-11-2004 10:41 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:16 PM.

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
Open Source Consulting | Domain Registration