Sending a dos environment variable to fstream instance

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2004
Posts: 48
Reputation: Coach_Nate is an unknown quantity at this point 
Solved Threads: 1
Coach_Nate's Avatar
Coach_Nate Coach_Nate is offline Offline
Light Poster

Sending a dos environment variable to fstream instance

 
0
  #1
May 3rd, 2007
Hi all,
I apologize if this is somewhere on here. I've looked some and I can't find it here or anywhere on the internet. With all the bright minds on this board, someone's bound to have an idea...

What I would like to do is open a file using the fstream class, but the pathname is going to be computer independent. In other words, I would like to call something of the following sort:
  1. ifstream file("%APPDATA%\\Folder\\file.txt");
  2. //do stuff...

Unfortunately, whenever I try using %APPDATA% or %HOMEPATH%, it seems to be sending something else to the ifstream's open method.

Any ideas/suggestions?
I appreciate any and all help - Thanks!
Behind every great gymnast - is a mentally unstable coach.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Sending a dos environment variable to fstream instance

 
0
  #2
May 3rd, 2007
Environment variables aren't expanded in string literals. You need to call something like getenv to get the value:
  1. #include <cstdlib>
  2.  
  3. //...
  4.  
  5. const char *appdata = std::getenv ( "APPDATA" );
  6.  
  7. if ( appdata != 0 ) {
  8. std::string path = appdata;
  9.  
  10. path += "\\Folder\\file.txt";
  11.  
  12. std::ifstream file ( path.c_str() );
  13.  
  14. //...
  15. }
Last edited by Narue; May 3rd, 2007 at 1:25 pm.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Join Date: Aug 2004
Posts: 48
Reputation: Coach_Nate is an unknown quantity at this point 
Solved Threads: 1
Coach_Nate's Avatar
Coach_Nate Coach_Nate is offline Offline
Light Poster

Re: Sending a dos environment variable to fstream instance

 
0
  #3
May 3rd, 2007
OH YEA! Thanks, Narue. I remember seeing the getenv function used somewhere else, but it's been a little while since I've done any actual c/c++ coding. Lately it's been mostly Java. But hey, Thanks again!
Behind every great gymnast - is a mentally unstable coach.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,868
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 755
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: Sending a dos environment variable to fstream instance

 
0
  #4
May 3rd, 2007
>Lately it's been mostly Java.
I'm sorry.
New members chased away this month: 5
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum


Views: 2252 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC