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;
    }
  • 相关阅读:
    模版的完全特化与偏特化
    [转]windows消息机制(MFC)
    MFC宏常识
    半透明AlphaBlend
    new、operator new、placement new
    DuplicateHandle
    Mac OS X 更新JAMF域控配置
    生成自签名CA+SSL证书
    Office 2016系列下载地址
    Spring Security静态资源访问
  • 原文地址:https://www.cnblogs.com/liulun/p/1572077.html
Copyright © 2011-2022 走看看