CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2007
    Posts
    2

    Windows Temp Dir

    This should be a very simple question, but I can't for the life of me find the answer to it. Quite simply, how can I query where the windows temp directory is?

    I'm making a program that will write a batch file in the temp directory when run off my thumb drive. I'm choosing the temp directory because users have access to their own temp directory at my school. Since this is going to go on my thumb drive, it needs to be versatile and not just point to C:\temp or C:\Documents and Settings\User Name\Local Settings\Temp.

    So far I've had some success, but with undesirable methods. So far what I've done is use the System function to save them evaluation of %temp% to a text file and then read that in to my variable. This is undesirable for two reasons: One that system is used, because I've read it's a naughty little function, and Two, because it's writing a file to my thumbdrive! (Thumbdrives have a limited lifespan based on the number of writes to it. And this sort of thing can add up over time).

    Quote Originally Posted by Code
    //includes
    #include <cstdlib>
    #include <iostream.h>
    #include <fstream.h>

    using namespace std;

    int main(int argc, char *argv[])
    {
    //write temp directory to file
    system("echo %temp% > tempDir");

    //read in temp directory
    ifstream TD("tempDir",ios::in);
    string tempDir;
    if(TD.is_open())
    {
    getline(TD, tempDir);
    TD.close();
    }
    //output it
    cout << "tempDir is " << tempDir << endl;

    system("pause"); //Oops, I did it again, well, it's a dev cpp thing
    return EXIT_SUCCESS;
    }
    So basically, how do I find where the windows temp directory is without writing anything to a file, and hopefully without calling system()?

  2. #2
    Join Date
    Jan 2003
    Posts
    615

    Re: Windows Temp Dir

    You could try getenv and use "TMP" as input.
    Before post, make an effort yourself, try googling or search here.

    When posting, give a proper description of your problem, include code* and error messages.

    *All code should include code tags

  3. #3
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Windows Temp Dir

    I think you Directly can read it from your environment variables.

    Thanx

  4. #4
    Join Date
    Mar 2007
    Posts
    2

    Re: Windows Temp Dir

    Quote Originally Posted by laasunde
    You could try getenv and use "TMP" as input.
    Thank you so much, that's exactly what I needed! Man, I simply could not find that anywhere. Now I've got it working like a charm (so far).
    Quote Originally Posted by humptydumpty
    I think you Directly can read it from your environment variables.
    I'm not exactly sure what you mean by that, but just typing in %temp% into my code doesn't work, I tried that before posting here.

  5. #5
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: Windows Temp Dir

    Use GetTempPath, I'm pretty sure getenv("TMP") isn't reliable. For example, there are guys that may not have defined this environment variable.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured