VC获取系统临时文件夹temp
一、介绍
获取临时文件夹路径GetTempPath, GetTempFileName获取临时文件夹下文件名
二、案例
// ConsoleApplication1.cpp : 定义控制台应用程序的入口点。
//#include "stdafx.h"#include "Windows.h"
/*
int main()
{HANDLE hFile = CreateFile(TEXT("text.txt"), GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, NULL);if (hFile == INVALID_HANDLE_VALUE){//MessageBox(TEXT("创建文件失败!"));return 1;}LARGE_INTEGER liDistanceToMove;liDistanceToMove.QuadPart = 102400000; //设置成这个大,单位字节if (!SetFilePointerEx(hFile, liDistanceToMove, NULL, FILE_BEGIN)){// MessageBox(TEXT("移动文件指针失败!"));}if (!SetEndOfFile(hFile)){// MessageBox(TEXT("设置文件尾失败!"));}CloseHandle(hFile);return 0;
}
*/
#include <windows.h>
#include <tchar.h>
#include <stdio.h>#define BUFSIZE 1024void PrintError(LPCTSTR errDesc);int _tmain(int argc, TCHAR *argv[])
{HANDLE hFile = INVALID_HANDLE_VALUE;HANDLE hTempFile = INVALID_HANDLE_VALUE;BOOL fSuccess = FALSE;DWORD dwRetVal = 0;UINT uRetVal = 0;DWORD dwBytesRead = 0;DWORD dwBytesWritten = 0;TCHAR szTempFileName[MAX_PATH];TCHAR lpTempPathBuffer[MAX_PATH];char chBuffer[BUFSIZE];LPCTSTR errMsg;if (argc != 2){_tprintf(TEXT("Usage: %s <file>\n"), argv[0]);return -1;}// Opens the existing file. hFile = CreateFile(argv[1], // 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){PrintError(TEXT("First CreateFile failed"));return (1);}// Gets the temp path env string (no guarantee it's a valid path).dwRetVal = GetTempPath(MAX_PATH, // length of the bufferlpTempPathBuffer); // buffer for path if (dwRetVal > MAX_PATH || (dwRetVal == 0)){PrintError(TEXT("GetTempPath failed"));if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}return (2);}_tprintf(TEXT(" %s\n"), lpTempPathBuffer);// Generates a temporary file name. uRetVal = GetTempFileName(lpTempPathBuffer, // directory for tmp filesTEXT("DEMO"), // temp file name prefix 0, // create unique name szTempFileName); // buffer for name _tprintf(TEXT(" %s\n"), szTempFileName);if (uRetVal == 0){PrintError(TEXT("GetTempFileName failed"));if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}return (3);}// Creates the new file to write to for the upper-case version.hTempFile = CreateFile((LPTSTR)szTempFileName, // file name GENERIC_WRITE, // open for write 0, // do not share NULL, // default security CREATE_ALWAYS, // overwrite existingFILE_ATTRIBUTE_NORMAL,// normal file NULL); // no template if (hTempFile == INVALID_HANDLE_VALUE){PrintError(TEXT("Second CreateFile failed"));if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}return (4);}// Reads BUFSIZE blocks to the buffer and converts all characters in // the buffer to upper case, then writes the buffer to the temporary // file. do{if (ReadFile(hFile, chBuffer, BUFSIZE, &dwBytesRead, NULL)){// Replaces lower case letters with upper case// in place (using the same buffer). The return// value is the number of replacements performed,// which we aren't interested in for this demo.CharUpperBuffA(chBuffer, dwBytesRead);fSuccess = WriteFile(hTempFile,chBuffer,dwBytesRead,&dwBytesWritten,NULL);if (!fSuccess){PrintError(TEXT("WriteFile failed"));return (5);}}else{PrintError(TEXT("ReadFile failed"));return (6);}// Continues until the whole file is processed.} while (dwBytesRead == BUFSIZE);// The handles to the files are no longer needed, so// they are closed prior to moving the new file.if (!CloseHandle(hFile)){PrintError(TEXT("CloseHandle(hFile) failed"));return (7);}if (!CloseHandle(hTempFile)){PrintError(TEXT("CloseHandle(hTempFile) failed"));return (8);}// Moves the temporary file to the new text file, allowing for differnt// drive letters or volume names.fSuccess = MoveFileEx(szTempFileName,TEXT("AllCaps.txt"),MOVEFILE_REPLACE_EXISTING | MOVEFILE_COPY_ALLOWED);if (!fSuccess){PrintError(TEXT("MoveFileEx failed"));return (9);}else_tprintf(TEXT("All-caps version of %s written to AllCaps.txt\n"), argv[1]);return (0);
}// ErrorMessage support function.
// Retrieves the system error message for the GetLastError() code.
// Note: caller must use LocalFree() on the returned LPCTSTR buffer.
LPCTSTR ErrorMessage(DWORD error)
{LPVOID lpMsgBuf;FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER| FORMAT_MESSAGE_FROM_SYSTEM| FORMAT_MESSAGE_IGNORE_INSERTS,NULL,error,MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),(LPTSTR)&lpMsgBuf,0,NULL);return((LPCTSTR)lpMsgBuf);
}// PrintError support function.
// Simple wrapper function for error output.
void PrintError(LPCTSTR errDesc)
{LPCTSTR errMsg = ErrorMessage(GetLastError());_tprintf(TEXT("\n** ERROR ** %s: %s\n"), errDesc, errMsg);LocalFree((LPVOID)errMsg);
}
VC获取系统临时文件夹temp相关推荐
- win7系统临时文件夹的打开方法
当我们在操作win7系统电脑或者浏览网页的时候,都会产生一些垃圾存放在临时文件夹里面,如果临时文件夹越大就会占用系统盘空间,从而影响系统运行速度,所以对临时文件夹进行清理是很有必要的,但是很多人并不知 ...
- 修改我的文档和临时文件夹temp的位置及系统优化知识
我们知道新装Windows在默认情况下,"我的文档"."收藏夹"等文件夹的存储位置都是在C:盘,这既不利于用户组织自己的文档,对系统的维护(包括重新安装)也造成 ...
- 笔记1:VC获取系统时间的方法
笔记1:VC 获取系统时间的方法 推荐的获取方法 获取时间方法种类 各种获取时间方法详解 方法1:time_t变量 和 time(time_t*)方法 方法2:system(" time&q ...
- java获取tomcat临时文件夹路径
/** 生成execl文件路径,tomcat临时文件夹temp **/ String path = request.getSession().getServletContext().getRealP ...
- VC++获取系统当前时间并显示
VC++6.0 获取系统时间 并输入格式化字符串 CTime tm = CTime::GetCurrentTime(); CString strtm= tm.Format("%Y-%m ...
- 服务器临时文件删除bat,bat 命令删除系统临时文件夹的内容
用批处理文件来删除临时文件 方法1 首先,新建一个记事本文件.再双击打开记事本,在其中输入以下代码 @echo off echo 正在清除系统垃圾文件,请稍等...... echo programme ...
- VC获取系统时间、程序运行时间
1.使用CTime类 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime(); str=tm.Format("现在时间是 ...
- VC++ 获取系统时间的方法汇总
1.使用CTime类(获取系统当前时间,精确到秒) ? 1 2 3 4 5 6 CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();/ ...
- 【VS开发】VC++ 获取系统时间、程序运行时间(精确到秒,毫秒)的五种方法
1.使用CTime类(获取系统当前时间,精确到秒) CString str; //获取系统时间 CTime tm; tm=CTime::GetCurrentTime();//获取系统日期 str=tm ...
- 获取用户临时文件夹路径
对文件操作,需要将文件临时存储在当前用户临时文件夹中: class Bt{public void LocalTempPath(){var tempPath = System.IO.Path.GetTe ...
最新文章
- python编程标准_python编程规范
- 计算机成绩统计优秀率,基于决策树算法的成绩优秀率分析与研究.pdf
- cheerio api
- 6-2 在一个数组中实现两个堆栈 (8 分)
- 3.卷1(套接字联网API)---套接字编程简介
- 发布PHP项目_Jenkins发布PHP项目之一自动化部署
- 线程中yield的用法
- .NET下解析Json的方法
- CE教程步骤8操作指南
- CENTOS 配置串口连接
- html 颜色渐变动画效果,js实现按钮颜色渐变动画效果
- 简单操作stm32f10xIO端口配置
- 软件测试工程师面试题之(计算机网络上)
- open_table和opened_table的区别
- 权威报告发布:细数OKEx OKChain公链那些“五星级”的骚操作
- 酷睿i7 12700k和i7 12700kf的区别 i712700k和i712700kf差多少
- 服务器信号有杂音怎么回事,电脑麦克风有杂音滋滋怎么解决
- 教程 | 零代码复制网页房源信息至表格中
- 使用IEEE 1394设备
- 图片标注工具LabelImg安装及使用
热门文章
- fydeos 安装linux程序,FydeOS安装教程-电脑系统安装手册
- Skipped ‘XXX.txt‘ -- Node remains in conflict 问题解决
- idea更新报Node remains in conflict冲突的解决办法
- Web Service简单demo
- allgro pcb铜皮编辑_Allegro设计PCB基础知识:快捷键设置、铜皮处理、DC-DC布局布线、电源分割等等...
- Arangodb集群web interface登陆认证设置
- java做微信支付notify_url异步通知服务端的写法
- 统计学基础——方差、协方差、标准差(标准偏差/均方差)、标准误、均方误差、均方根误差(标准误差)的区别
- 什么是公有云、私有云、混合云? 1
- 企业大数据价值你只发掘了1%!