createfile 与 writefile
函数原型:
HANDLE CreateFile(
LPCTSTR lpFileName, //指向文件名的指针
DWORD dwDesiredAccess, //访问模式(写/读)
DWORD dwShareMode, //共享模式
LPSECURITY_ATTRIBUTES lpSecurityAttributes, //指向安全属性的指针
DWORD dwCreationDisposition, //如何创建
DWORD dwFlagsAndAttributes, //文件属性
HANDLE hTemplateFile //用于复制文件句柄
);
参数列表
lpFileName String 要打开的文件的名字
dwDesiredAccess Long 如果为 GENERIC_READ 表示允许对设备进行读访问;如果为 GENERIC_WRITE 表示允许对设备进行写访问(可组合使用);如果为零,表示只允许获取与一个设备有关的信息
dwShareMode Long, 零表示不共享; FILE_SHARE_READ 和/或 FILE_SHARE_WRITE 表示允许对文件进行共享访问
lpSecurityAttributes SECURITY_ATTRIBUTES, 指向一个SECURITY_ATTRIBUTES结构的指针,定义了文件的安全特性(如果操作系统支持的话)
dwCreationDisposition Long,下述常数之一:
CREATE_NEW 创建文件;如文件存在则会出错
CREATE_ALWAYS 创建文件,会改写前一个文件
OPEN_EXISTING 文件必须已经存在。由设备提出要求
OPEN_ALWAYS 如文件不存在则创建它
TRUNCATE_EXISTING 讲现有文件缩短为零长度
dwFlagsAndAttributes Long, 一个或多个下述常数
FILE_ATTRIBUTE_ARCHIVE 标记归档属性
FILE_ATTRIBUTE_COMPRESSED 将文件标记为已压缩,或者标记为文件在目录中的默认压缩方式
FILE_ATTRIBUTE_NORMAL 默认属性
FILE_ATTRIBUTE_HIDDEN 隐藏文件或目录
FILE_ATTRIBUTE_READONLY 文件为只读
FILE_ATTRIBUTE_SYSTEM 文件为系统文件
FILE_FLAG_WRITE_THROUGH 操作系统不得推迟对文件的写操作
FILE_FLAG_OVERLAPPED 允许对文件进行重叠操作
FILE_FLAG_NO_BUFFERING 禁止对文件进行缓冲处理。文件只能写入磁盘卷的扇区块
FILE_FLAG_RANDOM_ACCESS 针对随机访问对文件缓冲进行优化
FILE_FLAG_SEQUENTIAL_SCAN 针对连续访问对文件缓冲进行优化
FILE_FLAG_DELETE_ON_CLOSE 关闭了上一次打开的句柄后,将文件删除。特别适合临时文件
也可在Windows NT下组合使用下述常数标记:
SECURITY_ANONYMOUS, SECURITY_IDENTIFICATION, SECURITY_IMPERSONATION, SECURITY_DELEGATION, SECURITY_CONTEXT_TRACKING, SECURITY_EFFECTIVE_ONLY
hTemplateFile Long, 如果不为零,则指定一个文件句柄。新文件将从这个文件中复制扩展属性
返回值
如执行成功,则返回文件句柄。
INVALID_HANDLE_VALUE表示出错,会设置GetLastError。即使函数成功,但若文件存在,且指定了CREATE_ALWAYS 或 OPEN_ALWAYS,GetLastError也会设为ERROR_ALREADY_EXISTS
(以上摘自百度百科)
实例:
1、直接在函数内指定文件地址:
void playCEwav()
{
CHAR *pBuffer;
DWORD RSize;
int fileSize = 0;
int i;
HANDLE hOpenFile = (HANDLE)CreateFile(L"E:\\a.text", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (hOpenFile == INVALID_HANDLE_VALUE)
{
hOpenFile = NULL;
MessageBoxA(NULL, "Can not open the file", "Playwav", MB_OK);
}
fileSize = GetFileSize(hOpenFile, NULL);
pBuffer = (CHAR *) malloc(fileSize);
ReadFile(hOpenFile, pBuffer, fileSize, &RSize, NULL);
//可将pBuffer显示在某区域或写入另一个文件来检查读出是否正确
free(pBuffer);
}
2、把文件地址通过参数传入:
void playwav(TCHAR * path)
{
CHAR *pBuffer;
DWORD RSize;
int fileSize = 0;
int i;
TCHAR szPath[100];
memset(szPath, 0, sizeof(szPath));
_tcscpy(szPath, path);
HANDLE hOpenFile = (HANDLE)CreateFile(szPath, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, NULL, NULL);
if (hOpenFile == INVALID_HANDLE_VALUE)
{
hOpenFile = NULL;
MessageBoxA(NULL, "Can not open the file", "Playwav", MB_OK);
}
fileSize = GetFileSize(hOpenFile, NULL);
pBuffer = (CHAR *) malloc(fileSize);
ReadFile(hOpenFile, pBuffer, fileSize, &RSize, NULL);
free(pBuffer);
}
#include <windows.h>
#include <stdio.h>
#define BUFSIZE 4096
int main()
{
HANDLE hFile;
HANDLE hTempFile;
DWORD dwBytesRead, dwBytesWritten, dwBufSize=BUFSIZE;
char szTempName[MAX_PATH];
char buffer[BUFSIZE];
char lpPathBuffer[BUFSIZE];
// 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("Could not open file.");
return 0;
}
// Get the temp path
GetTempPath(dwBufSize, // length of the buffer
lpPathBuffer); // buffer for path
// Create a temporary file.
GetTempFileName(lpPathBuffer, // directory for temp files
"NEW", // temp file name prefix
0, // create unique name
szTempName); // buffer for name
hTempFile = CreateFile((LPTSTR) szTempName, // file name
GENERIC_READ | GENERIC_WRITE, // open for read/write
0, // do not share
NULL, // default security
CREATE_ALWAYS, // overwrite existing file
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no template
if (hTempFile == INVALID_HANDLE_VALUE)
{
printf("Could not create temporary file.");
return 0;
}
// Read 4K 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, 4096,
&dwBytesRead, NULL))
{
CharUpperBuff(buffer, dwBytesRead);
WriteFile(hTempFile, buffer, dwBytesRead,
&dwBytesWritten, NULL);
}
} while (dwBytesRead == BUFSIZE);
// Close both files.
CloseHandle(hFile);
CloseHandle(hTempFile);
// Move the temporary file to the new text file.
if (!MoveFileEx(szTempName,
"allcaps.txt",
MOVEFILE_REPLACE_EXISTING))
{
printf("Could not move temp file.");
return 0;
}
}