WindowsXp文件读写模块

本文介绍了一个用于Windows XP系统的文件操作模块,该模块提供了一系列API,包括创建文件以供读写、设置文件指针位置、获取当前文件指针位置及读写文件等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//WindowsXp文件读写模块
//FileOp.h
//////////////////////////////////////////////////////

#ifndef __FILE_OP_H__
#define __FILE_OP_H__

//////////////////////////////////////////////////////

#include <Windows.h>

//////////////////////////////////////////////////////

#define FILE_OP_API __declspec(dllexport)

//////////////////////////////////////////////////////

FILE_OP_API HANDLE _CreateFileForRead(const wchar_t* _lpszFileName);
FILE_OP_API HANDLE _CreateFileForWrite(const wchar_t* _lpszFileName);
FILE_OP_API void _SetFilePointerToBegin(HANDLE _hFile);
FILE_OP_API void _SetFilePointerToEnd(HANDLE _hFile);
FILE_OP_API void _SetFilePointerToPos(HANDLE _hFile,LONG _lPos);
FILE_OP_API long _GetFilePointerPos(HANDLE _hFile);
FILE_OP_API long _ReadFile(HANDLE _hFile,void* _lpData,long _lSize);
FILE_OP_API long _WriteFile(HANDLE _hFile,void* _lpData,long _lSize);
FILE_OP_API void _CloseFile(HANDLE _hFile);

//////////////////////////////////////////////////////

#endif//__FILE_OP_H__

//////////////////////////////////////////////////////
//FileOp.cpp
//////////////////////////////////////////////////////

#include <windows.h>
#include "FileOp.h"
#include <assert.h>

//////////////////////////////////////////////////////

FILE_OP_API HANDLE _CreateFileForRead(const wchar_t* _lpszFileName)
{
 int iCreateFileCount = 0;
 HANDLE hFile = INVALID_HANDLE_VALUE;

 iCreateFileCount = 0;
 do
 {
  if (0 < iCreateFileCount)
  {
   Sleep(10);
  }
  
  hFile = CreateFile(_lpszFileName,GENERIC_READ,FILE_SHARE_READ,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
  iCreateFileCount ++;
 }while (INVALID_HANDLE_VALUE == hFile && iCreateFileCount < 5);
 assert (INVALID_HANDLE_VALUE != hFile);

 if (INVALID_HANDLE_VALUE == hFile)
 {
  hFile = NULL;
 }

 return hFile;
}

FILE_OP_API HANDLE _CreateFileForWrite(const wchar_t* _lpszFileName)
{
 int iCreateFileCount = 0;
 HANDLE hFile = INVALID_HANDLE_VALUE;

 iCreateFileCount = 0;
 do
 {
  if (0 < iCreateFileCount)
  {
   Sleep(10);
  }
  
  hFile = CreateFile(_lpszFileName,GENERIC_WRITE,FILE_SHARE_READ,0,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
  iCreateFileCount ++;
 }while (INVALID_HANDLE_VALUE == hFile && iCreateFileCount < 5);
 assert (INVALID_HANDLE_VALUE != hFile);

 if (INVALID_HANDLE_VALUE == hFile)
 {
  hFile = NULL;
 }

 return hFile;
}

FILE_OP_API void _SetFilePointerToBegin(HANDLE _hFile)
{
 if (NULL != _hFile)
 {
  SetFilePointer(_hFile,0,0,FILE_BEGIN);
 }
}

FILE_OP_API void _SetFilePointerToEnd(HANDLE _hFile)
{
 if (NULL != _hFile)
 {
  SetFilePointer(_hFile,0,0,FILE_END);
 }
}

FILE_OP_API void _SetFilePointerToPos(HANDLE _hFile,LONG _lPos)
{
 if (NULL != _hFile)
 {
  SetFilePointer(_hFile,_lPos,0,FILE_BEGIN);
 }
}

FILE_OP_API long _GetFilePointerPos(HANDLE _hFile)
{
 if (NULL != _hFile)
 {
  return SetFilePointer(_hFile,0,0,FILE_CURRENT);
 }

 return 0;
}

FILE_OP_API long _ReadFile(HANDLE _hFile,void* _lpData,long _lSize)
{
 BOOL rel = FALSE;
 DWORD dwNumberOfBytesRead = 0;

 if (NULL != _hFile)
 {
  rel = ReadFile(_hFile,_lpData,_lSize,&dwNumberOfBytesRead,0);
  assert (rel);
  if (rel)
  {
   return dwNumberOfBytesRead;
  }
  return -1;
 }

 return -2;
}

FILE_OP_API long _WriteFile(HANDLE _hFile,void* _lpData,long _lSize)
{
 BOOL rel = FALSE;
 DWORD dwNumberOfBytesWritten = 0;

 if (NULL != _hFile)
 {
  rel = WriteFile(_hFile,_lpData,_lSize,&dwNumberOfBytesWritten,0);
  assert (rel);
  if (rel)
  {
   return dwNumberOfBytesWritten;
  }

  return -1;
 }

 return -2;
}

FILE_OP_API void _CloseFile(HANDLE _hFile)
{
 if (NULL != _hFile)
 {
  CloseHandle(_hFile);
  _hFile = NULL;
 }
}

//////////////////////////////////////////////////////

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值