zoukankan      html  css  js  c++  java
  • WinApi学习笔记读写文件

    读文件

    #include <windows.h>
    #include <stdio.h>
    #include <iostream>
    
    DWORD MyReadFile(LPSTR filePath)
    {
    	HANDLE hFileRead;
    	LARGE_INTEGER liFileSize;
    	DWORD dwReadSize;
    	LONGLONG liTotalRead = 0;
    	BYTE lpFileDataBuffer[32];
    
    	hFileRead = CreateFile(
    		filePath,
    		GENERIC_READ,
    		FILE_SHARE_READ,
    		NULL,
    		OPEN_ALWAYS,//有就打开,没有就创建
    		FILE_ATTRIBUTE_NORMAL,
    		NULL
    		);
    	if(hFileRead == INVALID_HANDLE_VALUE)
    	{
    		printf("open error");
    	}
    	if(!GetFileSizeEx(hFileRead,&liFileSize))
    	{
    		printf("get size error");
    	}
    	else
    	{
    		printf("size is %d\n",liFileSize.QuadPart);//???
    	}
    	while(true)
    	{
    		DWORD i;
    		if(!ReadFile(hFileRead,lpFileDataBuffer,32,&dwReadSize,NULL))
    		{
    			printf("error while reading");
    		}
    		printf("read %d byte\n",&dwReadSize);
    		for(i = 0;i<dwReadSize;i++)
    		{
    			printf("  0x%x   ",lpFileDataBuffer[i]);
    			std::cout<<lpFileDataBuffer[i]<<std::endl;
    		}
    		printf("\n");
    		liTotalRead += dwReadSize;
    		if(liTotalRead == liFileSize.QuadPart)
    		{
    			printf("end");
    			break;
    		}
    	}
    	CloseHandle(hFileRead);
    	return 0;
    }
    int main()
    {
    	MyReadFile("c:\\a.txt");
    	char a;
    	std::cin >> a;
    	return 0;
    }



    写文件

    #include <windows.h>
    #include <stdio.h>
    #include <iostream>
    
    DWORD MyWriteFile(LPSTR filePath,LPVOID lpData,DWORD dwSize)
    {
    	HANDLE hWrite;
    	DWORD dwWriteSize;
    	hWrite = CreateFile(filePath,GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    	if(hWrite == INVALID_HANDLE_VALUE)
    	{
    		printf("open error");
    	}
    	SetFilePointer(hWrite,0,0,FILE_END);//??
    	if(!WriteFile(hWrite,lpData,dwSize,&dwWriteSize,NULL))
    	{
    		printf("error while writeing");
    	}
    	CloseHandle(hWrite);
    	return 0;
    }
    int main()
    {
    	MyWriteFile("c:\\a.txt","myxland",lstrlen("myxland"));
    	char a;
    	std::cin >> a;
    	return 0;
    }
  • 相关阅读:
    SAP S/4HANA extensibility扩展原理介绍
    SAP CRM系统订单模型的设计与实现
    使用nodejs代码在SAP C4C里创建Individual customer
    SAP Cloud for Customer Account和individual customer的区别
    Let the Balloon Rise map一个数组
    How Many Tables 简单并查集
    Heap Operations 优先队列
    Arpa’s obvious problem and Mehrdad’s terrible solution 思维
    Passing the Message 单调栈两次
    The Suspects 并查集
  • 原文地址:https://www.cnblogs.com/liulun/p/1572077.html
Copyright © 2011-2022 走看看