#include<iostream>
#include<windows.h>
#include<stdio.h>
using namespace std; int main22(int argc,char* argv[])
{
//创建文件
//lpFileName:将要打开的串口逻辑名,如"COM1";
//dwDesiredAccess:指定串口访问的类型,可以是读取、写入或二者并列;
//dwShareMode:指定共享属性,由于串口不能共享,该参数必须置为0;
//lpSecurityAttributes:引用安全性属性结构,缺省值为NULL;
//dwCreationDistribution:创建标志,对串口操作该参数必须置为OPEN_EXISTING;
//dwFlagsAndAttributes:属性描述,用于指定该串口是否进行异步操作,该值为FILE_FLAG_OVERLAPPED,表示使用异步的I/O;该值为0,表示同步I/O操作;
//hTemplateFile:对串口而言该参数必须置为NULL。
HANDLE hFILE=CreateFile("1.txt",GENERIC_WRITE|GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(hFILE == INVALID_HANDLE_VALUE)
{
cout << "create file error " << endl;
return 0;
} ////写文件
//char buff[] = "xiongyungang3";
//DWORD dwWrite; //if(!WriteFile(hFILE,&buff,sizeof(buff),&dwWrite,NULL))
//{
// cout << "write file error " << endl;
// return 0;
//} //写完文件直接读取会出错
//读文件
int file_size = 0;
file_size = GetFileSize(hFILE,NULL);
cout << file_size << endl;
char *readbuff;
readbuff = (char*)malloc(file_size);
DWORD dwRead;
if(!ReadFile(hFILE,readbuff,file_size,&dwRead,NULL))
{
cout << "read file error" << endl;
return 0;
}
readbuff[file_size] = '\0';
printf("%s\n",readbuff); //关闭文件
CloseHandle(hFILE); system("pause");
return 0;
}
04-30 12:49