Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost filesystem get_temporary_file()
From: Alexander Lamaison (awl03_at_[hidden])
Date: 2009-07-17 15:46:59


On Thu, 16 Jul 2009 19:33:19 +0100, Craig Henderson wrote:

> I cannot find any function in the Boost.Filesystem library to get a
> temporary filename. There has been some discussion about this functionality
> on the lists many years ago - did anything come of this? I need a
> cross-platform way to do this and was hoping boost would provide it :)

I was looking for exactly such a thing just yesterday and was surprised to
find that boost::filesystem didn't have one. Here's the function I wrote
instead (Windows only) in case it's of any use:

wpath TempFilePath()
{
        vector<wchar_t> buffer(MAX_PATH);
        DWORD len = ::GetTempPath(buffer.size(), &buffer[0]);
                
        wstring directory(&buffer[0], buffer.size());

        if (!GetTempFileName(directory.c_str(), NULL, 0, &buffer[0]))
                throw boost::system::system_error(
                        ::GetLastError(), boost::system::system_category);

        wstring file(&buffer[0], buffer.size());
                
        return wpath(directory) / file;
}

Alex


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net