| | |
Sending a dos environment variable to fstream instance
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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!
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)
ifstream file("%APPDATA%\\Folder\\file.txt"); //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.
Environment variables aren't expanded in string literals. You need to call something like getenv to get the value:
C++ Syntax (Toggle Plain Text)
#include <cstdlib> //... const char *appdata = std::getenv ( "APPDATA" ); if ( appdata != 0 ) { std::string path = appdata; path += "\\Folder\\file.txt"; std::ifstream file ( path.c_str() ); //... }
Last edited by Narue; May 3rd, 2007 at 1:25 pm.
New members chased away this month: 5
![]() |
Similar Threads
- Pointers (C++)
- Variable Variable Names (C)
- Need DOS help (Windows 95 / 98 / Me)
- Pointers (Part II) (C)
- XP style in DOS ! (C)
- More newbie problems - VBScript and Env variables (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Is There a way to create a GUI in C++
- Next Thread: loop help
Views: 2252 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






