createFile

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

转载于:https://www.cnblogs.com/xiongyungang/p/10715398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值