Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to locate temp folder

Status
Not open for further replies.

rachelason

Programmer
Jun 28, 2004
114
GB
hello,
I want to create a file in users temp folder. Is there any c++ code to locate temp folder?

thanks
 
yes there is
Code:
char theTempPath[MAX_PATH];
::GetTempPath(MAX_PATH,theTempPath);
O.B
 
HI Thanks for that. Is there any online material available to explain your code? I tried MSDN...no good.
I found this code from codeguru.
<code>
using namespace System::Security;
using namespace System::IO;

...

String tempFolder;
try
{
tempFolder = Path::GetTempPath();
}
catch(SecurityException* ex)
{
// probably means that you don't have the required permissions
}
catch(Exception* ex)
{
// handle all other exceptions
}

</code> (by the way how do i put the block of code in code block as you have done?)

When i use the same in my program it says
'System' : is not a class or namespace name
'Security' : a namespace with this name does not exist
etc...
any help on this?
thanks


 
I managed to find out that the code i posted is .net framework which is not what i am using.
Any more help on getting temporary path using just c++?
thanks
 
Hi, THanks for you reply.
I tried to use the example from the firstlink you have provided, but i don't seem to understand how i can use it in my query.

This is the example from the msdn site. it first picks up where the orignial.txt file is, then creates a allcaps.txt in the same directory. But if the original.txt is not in the working directory(where the visual studio file is), then it doesn't create the allcaps file, even if you provide the full path of the original.txt.
I don't understand the purpose of this program or I am not sure how to use the bit i need from this example.
thanks

<code>
#include <windows.h>
#include <stdio.h>
#define BUFSIZE 65536

int main()
{
HANDLE hFile;
HANDLE hTempFile;
DWORD dwRetVal;
DWORD dwBytesRead;
DWORD dwBytesWritten;
DWORD dwBufSize=BUFSIZE;
UINT uRetVal;
char szTempName[BUFSIZE];
char buffer[BUFSIZE];
char lpPathBuffer[BUFSIZE];
BOOL fSuccess;

// Open the existing file.
hFile = CreateFile("original.txt", // file name
GENERIC_READ, // open for reading
0, // do not share
NULL, // default security
OPEN_EXISTING, // existing file only
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hFile == INVALID_HANDLE_VALUE)
{
printf ("CreateFile failed with error %d.\n",
GetLastError());
return (1);
}

// Get the temp path.
dwRetVal = GetTempPath(dwBufSize, // length of the buffer
lpPathBuffer); // buffer for path
if (dwRetVal > dwBufSize)
{
printf ("GetTempPath failed with error %d.\n",
GetLastError());
return (2);
}

// Create a temporary file.
uRetVal = GetTempFileName(lpPathBuffer, // directory for tmp files
"NEW", // temp file name prefix
0, // create unique name
szTempName); // buffer for name
if (uRetVal == 0)
{
printf ("GetTempFileName failed with error %d.\n",
GetLastError());
return (3);
}

// Create the new file to write the upper-case version to.
hTempFile = CreateFile((LPTSTR) szTempName, // file name
GENERIC_READ | GENERIC_WRITE, // open r-w
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing
FILE_ATTRIBUTE_NORMAL,// normal file
NULL); // no template
if (hTempFile == INVALID_HANDLE_VALUE)
{
printf ("CreateFile failed with error %d.\n",
GetLastError());
return (4);
}

// Read BUFSIZE blocks to the buffer. Change all characters in
// the buffer to upper case. Write the buffer to the temporary
// file.
do
{
if (ReadFile(hFile,
buffer,
BUFSIZE,
&dwBytesRead,
NULL))
{
CharUpperBuff(buffer, dwBytesRead);
fSuccess = WriteFile(hTempFile,
buffer,
dwBytesRead,
&dwBytesWritten,
NULL);
if (!fSuccess)
{
printf ("WriteFile failed with error %d.\n",
GetLastError());
return (5);
}
}
else
{
printf ("ReadFile failed with error %d.\n",
GetLastError());
return (6);
}
} while (dwBytesRead == BUFSIZE);

// Close the handles to the files.
fSuccess = CloseHandle (hFile);
if (!fSuccess)
{
printf ("CloseHandle failed with error %d.\n",
GetLastError());
return (7);
}
fSuccess = CloseHandle (hTempFile);
if (!fSuccess)
{
printf ("CloseHandle failed with error %d.\n",
GetLastError());
return (8);
}

// Move the temporary file to the new text file.
fSuccess = MoveFileEx(szTempName,
"allcaps.txt",
MOVEFILE_REPLACE_EXISTING);
if (!fSuccess)
{
printf ("MoveFileEx failed with error %d.\n", GetLastError());
return (9);
}
return (0);
}

</code>
 
These are the brains of the thing:
Code:
   // Get the temp path.
    dwRetVal = GetTempPath(dwBufSize,     // length of the buffer
                           lpPathBuffer); // buffer for path 
    if (dwRetVal > dwBufSize)
    {
        printf ("GetTempPath failed with error %d.\n", 
                GetLastError());
        return (2);
    }

    // Create a temporary file. 
    uRetVal = GetTempFileName(lpPathBuffer, // directory for tmp files
                              "NEW",        // temp file name prefix 
                              0,            // create unique name 
                              szTempName);  // buffer for name 
    if (uRetVal == 0)
    {
        printf ("GetTempFileName failed with error %d.\n", 
                GetLastError());
        return (3);
    }

    // Create the new file to write the upper-case version to.
    hTempFile = CreateFile((LPTSTR) szTempName, // file name 
                           GENERIC_READ | GENERIC_WRITE, // open r-w 
                           0,                    // do not share 
                           NULL,                 // default security 
                           CREATE_ALWAYS,        // overwrite existing
                           FILE_ATTRIBUTE_NORMAL,// normal file 
                           NULL);                // no template 
    if (hTempFile == INVALID_HANDLE_VALUE) 
    { 
        printf ("CreateFile failed with error %d.\n", 
                GetLastError());
        return (4);
    }

The call to "GetTempPath" fills up a buffer with the path to the system defined temporary directory.

The call to "GetTempFileName" fills up another buffer with the path to the temporary directory PLUS a filename created by rules you specify. In the example, the filename will be NEW plus a random number and the result is stored in szTempName.

The call to "CreateFile" actually opens the file. Do what you will with it, but don't assume this file will live forever, since it's in the temp directory.

It doesn't even have an extension, so if you decide you need to keep this file, you'll have to rewind it, read the data back out, and write it to your permanent file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top