944,098 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3079
  • C++ RSS
May 3rd, 2007
0

Sending a dos environment variable to fstream instance

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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!
Similar Threads
Reputation Points: 14
Solved Threads: 1
Light Poster
Coach_Nate is offline Offline
48 posts
since Aug 2004
May 3rd, 2007
0

Re: Sending a dos environment variable to fstream instance

Environment variables aren't expanded in string literals. You need to call something like getenv to get the value:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
May 3rd, 2007
0

Re: Sending a dos environment variable to fstream instance

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!
Reputation Points: 14
Solved Threads: 1
Light Poster
Coach_Nate is offline Offline
48 posts
since Aug 2004
May 3rd, 2007
0

Re: Sending a dos environment variable to fstream instance

>Lately it's been mostly Java.
I'm sorry.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Is There a way to create a GUI in C++
Next Thread in C++ Forum Timeline: loop help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC