HANDLE CreateFile(
LPCTSTR,lpFileName, //指向文件名的指针
DWORD dwDesiredAccess, //访问模式(读/写)
DWORD dwShareMode, //共享模式
LPSECURITY_ATTRIBUTES lpSecurityAttributes,//指向安全属性的指针
DWORD dwCreationDisposition, //如何让创建
DWORD dwFlagAndAttributes, //文件属性
HANDLE hTemplateFile //用于复制文件句柄
);
BOOL WriteFile(
HANDLE fFile, //文件句柄
LPCVOID lpBuffer, //数据缓存区指针
DWORD nNumberOfBytesToWrite, //所要写的字节数
LPDWORD lpNumberOfBytesWritten,//用于保存实际写入字节数的存储区的指针
LPOVERLAPPED lpOverlapped //OVERLAPPED结构体指针
)
BOOL ReadFile(
HANDLE fFile, //文件句柄
LPCVOID lpBuffer, //数据缓存区指针
DWORD nNumberOfBytesToRead, //所要写的字节数
LPDWORD lpNumberOfBytesRead, //用于保存实际写入字节数的存储区的指针
LPOVERLAPPED lpOverlapped //OVERLAPPED结构体指针
)
//写文件
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE hFILE=CreateFile("1.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFILE==INVALID_HANDLE_VALUE)
{
printf("CreateFile error\n");
return 0;
}
if(SetFilePointer(hFILE,0,NULL,FILE_END)==-1)
{
printf("SetFilePointer error\n");
return 0;
}
char buff[256]="hello";
DWORD dwWrite;
if(!WriteFile(hFILE,&buff,strlen(buff),&dwWrite,NULL))
{
printf("WriteFile error\n");
return 0;
}
printf("write %d.\n",dwWrite);
printf("done.\n");
CloseHandle(hFILE);
return 0;
}
//读取文件
#include <stdio.h>
#include <windows.h>
int main()
{
HANDLE hFile=CreateFile("1.txt",GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFile==INVALID_HANDLE_VALUE)
{
printf("CreateFile error.\n");
return 0;
}
int file_size=0;
file_size=GetFileSize(hFile,NULL);
char *buff;
buff=(char*)malloc(file_size);
DWORD dwRead;
if(!ReadFile(hFile,buff,file_size,&dwRead,NULL))
{
printf("ReadFile error.\n");
return 0;
}
buff[file_size]='\0';
printf("content:%s\n",buff);
CloseHandle(hFile);
return 0;
}
createFile
最新推荐文章于 2023-02-02 00:29:06 发布