%systemroot%

Hi all. What would be a way to program a code that created txt files in any dir after %systemroot% ?

For example. If you were to type cd %systemroot% in an XP command line, then you would get sent to C:\WINDOWS - Im trying to find out a way to create files in that directory, no matter the drive. Ive tried the following code:

#include <fstream>
using namespace std;

void main(){
ofstream file;
file.open("%systemroot%\\test.txt");
file << "This is only a test." << endl;
file.close();
}

And nothing happens. So i switched %systemroot% to %windir% from another persons example, and it didnt work either. So i was hoping maybe im using a wrong character. (ie: %) Maybe if im using another character then i'll succeed. Any suggestions will be greatly appreciated. Thanx in advance!
[866 byte] By [dellthinker] at [2007-11-20 7:50:07]
# 1 Re: %systemroot%
In general you'll want to use the api's to retrieve info such as this rather than the 'system' functions.

Check out the following in MSDN:

GetWindowsDirectory
GetSystemDirectory
SHGetFolderPath
Arjay at 2007-11-10 22:57:23 >
# 2 Re: %systemroot%
Or if you insist on using %systemroot%, then you can read the value of the environment variable named 'SystemRoot' and use it. When you use % on the command prompt, it tells the command prompt to get the path from the environment variable. Looks like the file open APIs do not directly support that notation. I guess the reason is that C++ per se is not tied to Windows, while the notion that % denotes a special path is Windows specific. May be if you use Windows APIs directly, they may support such special characters.

Did you check to see if your program did actually create a directory named '%systemroot%' (literally) inside the directory from where you ran the program?
UnderDog at 2007-11-10 22:58:23 >