#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;
}