Skip to content

Instantly share code, notes, and snippets.

@SteffenL
Created April 3, 2013 18:36
Show Gist options
  • Save SteffenL/5303896 to your computer and use it in GitHub Desktop.
Save SteffenL/5303896 to your computer and use it in GitHub Desktop.
Opening a file using the path returned by tmpnam() may result in a "permission denied" error on Windows. This sample helps illustrate the problem. Make sure to open the Visual Studio command prompt before running build-all.bat.
@echo off
echo Locating tools...
where /Q g++
if not %errorlevel% == 0 echo "g++" not found.& goto :eof
where /Q cl
if not %errorlevel% == 0 echo "cl" not found.& goto :eof
where /Q msbuild
if not %errorlevel% == 0 echo "msbuild" not found.& goto :eof
set outDir=bin
if not exist "%outDir%" md "%outDir%"
if not %errorlevel% == 0 echo Couldn't create directory: %outDir%& goto :eof
echo Building...
g++ -o "%outDir%\test-gcc" main.cpp > nul
if not %errorlevel% == 0 goto :eof
cl "/Fe%outDir%\test-msvc" /EHsc /nologo main.cpp > nul
if not %errorlevel% == 0 goto :eof
msbuild /nologo "/property:Configuration=Release;OutDir=%outDir%" > nul
if not %errorlevel% == 0 goto :eof
echo Running...
pushd "%outDir%"
test-gcc
test-msvc
test-msvc-ide
popd
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <cstring>
#include <string>
using std::cout;
using std::endl;
using std::ios;
void test1()
{
cout << "--- Test #1: tmpnam() ---" << endl;
char* tempFilePath = tmpnam(0);
cout << "Temp file path: " << tempFilePath << endl;
std::fstream file(tempFilePath, ios::in | ios::out | ios::binary | ios::trunc);
if (!file.is_open()) {
cout << strerror(errno) << endl << endl;
return;
}
file.close();
remove(tempFilePath);
cout << "OK" << endl << endl;
return;
}
void test2()
{
cout << "--- Test #2: _tempnam() ---" << endl;
char* tempFilePath_c = _tempnam(0, 0);
const std::string tempFilePath(tempFilePath_c);
free(tempFilePath_c);
cout << "Temp file path: " << tempFilePath << endl;
std::fstream file(tempFilePath.c_str(), ios::in | ios::out | ios::binary | ios::trunc);
if (!file.is_open()) {
cout << strerror(errno) << endl << endl;
return;
}
file.close();
remove(tempFilePath.c_str());
cout << "OK" << endl << endl;
return;
}
int main(int argc, char* argv[])
{
cout << "==================== " << argv[0] << " ====================" << endl;
char* tempEnvVar = getenv("TMP");
cout << "TMP: " << tempEnvVar << endl << endl;
test1();
test2();
}
Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test", "test.vcxproj", "{E0472BC4-D9F0-4257-9A3A-D2D750D095E9}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{E0472BC4-D9F0-4257-9A3A-D2D750D095E9}.Debug|Win32.ActiveCfg = Debug|Win32
{E0472BC4-D9F0-4257-9A3A-D2D750D095E9}.Debug|Win32.Build.0 = Debug|Win32
{E0472BC4-D9F0-4257-9A3A-D2D750D095E9}.Release|Win32.ActiveCfg = Release|Win32
{E0472BC4-D9F0-4257-9A3A-D2D750D095E9}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{E0472BC4-D9F0-4257-9A3A-D2D750D095E9}</ProjectGuid>
<Keyword>Win32Proj</Keyword>
<RootNamespace>test</RootNamespace>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v110</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>MultiByte</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental>
<OutDir>$(SolutionDir)bin\</OutDir>
<TargetName>test-msvc-ide</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)bin\</OutDir>
<TargetName>test-msvc-ide</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>
</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalDependencies />
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>
</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="main.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment