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;
    }
  • 相关阅读:
    [转]java常量池理解总结
    设计数据库时使用外键吗
    ajax 下载文件
    CentOS6.4配置163的yum源
    Xms Xmx PermSize MaxPermSize 区别
    [转]基于java的程序OutOfMemory问题的解决及Xms/Xmx/Xss的解释和应用
    Java Web Services面试
    java 单例模式
    Spring和MyBatis整合
    Spring核心概念
  • 原文地址:https://www.cnblogs.com/liulun/p/1572077.html
Copyright © 2011-2022 走看看