mkstemp有对应的window版本吗

dongle2001 2011-04-02 02:50:21
mkstemp是linux的创建临时文件的函数。我想知道这个函数在windows下可用吗,如果不能用的话,哪位帮忙写一个window版本的mkstemp,在下感激不尽。
...全文
175 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dongle2001 2011-04-04
  • 打赏
  • 举报
回复
主要是想实现mkstemp的模板功能,ls的代码太简单点了。
luxingqiang110 2011-04-02
  • 打赏
  • 举报
回复
#include <Windows.h>
#include <stdio.h>
#define BUFSIZE 512

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 || (dwRetVal == 0))
{
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);
}

64,649

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧