유용한 정보

[C++]2Gb 이상의 파일 처리

DevReff 2024. 12. 26. 19:41




728x90
반응형

// LargeFileIO.h

#ifndef __LARGE_FILEIO__
#define __LARGE_FILEIO__

 

typedef BOOL WINAPI pGetFileSizeEx(HANDLE hFile, PLARGE_INTEGER lpFileSize);
typedef BOOL WINAPI pSetFilePointerEx( HANDLE hFile,
LARGE_INTEGER liDistanceToMove,
PLARGE_INTEGER lpNewFilePointer,
DWORD dwMoveMethod);

 

void initFILE(); // 프로그램의 시작부분에서 호출해야함

 

class LargeFile
{
public:

LargeFile();
bool OpenR(const char* name, DWORD access = GENERIC_READ,
DWORD share = FILE_SHARE_READ, DWORD create= OPEN_ALWAYS);

bool OpenW(const char* name, DWORD access = GENERIC_WRITE | GENERIC_READ,
DWORD share = FILE_SHARE_WRITE | FILE_SHARE_READ, DWORD create= OPEN_ALWAYS);

void Close();
UINT64 GetLength() ;
void SeekToBegin() ;

void SeekToEnd();

void Seek(UINT64 dis);

UINT Read(char* buffer, UINT len);
UINT Read(char* buffer, UINT len, DWORD* r);

void Write(char*buffer, UINT len);

HANDLE hFile;
};

#endif

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

// LargeFileIO.cpp

#include "stdafx.h"
#include "LargeFileIO.h"


#ifdef _CAN_UPLOAD_CASE_MAX2GB_
pGetFileSizeEx* lpGetFileSizeEx = NULL;
pSetFilePointerEx * lpSetFilePointerEx = NULL;
#endif

void initFILE()
{
HMODULEunzipDll;
if ((unzipDll = (HMODULE)LoadLibrary(_T("kernel32.dll"))))
{
TRACE("[initFILE] : kernel32.dll handle =0x%08x\n", unzipDll);
}

#ifdef _CAN_UPLOAD_CASE_MAX2GB_
lpGetFileSizeEx = (pGetFileSizeEx *)GetProcAddress(unzipDll, "GetFileSizeEx");
lpSetFilePointerEx = (pSetFilePointerEx *)GetProcAddress(unzipDll, "SetFilePointerEx");
#endif
}

UINT LargeFile::Read(char* buffer, UINT len, DWORD* r)
{
return ReadFile(hFile, buffer, len, r, NULL);
}


LargeFile::LargeFile()
{
hFile= INVALID_HANDLE_VALUE;
}


bool LargeFile::OpenR(const char* name, DWORD access,
DWORD share, DWORD create)
{

// 이렇게 local 변수로 안하면 알수 없는 이유로 CreateFile함수에서 에러가 발생한다.

char buf[1000];
strcpy_s(buf, sizeof(buf), name);
hFile = CreateFileA(buf, access, share, NULL, create, FILE_ATTRIBUTE_NORMAL, NULL);

if ( hFile == INVALID_HANDLE_VALUE ) {
int error = GetLastError();
char buf[10];
_itoa(error, buf, 10);
TRACE("[LargeFile::OpenR]:reading error\n");
return false;


return false;
}
return true;
}

bool LargeFile::OpenW(const char* name, DWORD access ,DWORD share, DWORD create)
{
hFile = CreateFileA(name, access, share, NULL, create, FILE_ATTRIBUTE_NORMAL, NULL);
if ( hFile == INVALID_HANDLE_VALUE ) {
int error = GetLastError();

char buf[10];
_itoa(error, buf, 10);
TRACE("writting error\n");
return false;
}
return true;
}

void LargeFile::Close() {
if ( hFile != INVALID_HANDLE_VALUE ) {
CloseHandle(hFile);
hFile = INVALID_HANDLE_VALUE;

}
}

UINT64 LargeFile::GetLength()
{
#ifndef _CAN_UPLOAD_CASE_MAX2GB_
unsigned int l;
l = GetFileSize(hFile, NULL);
return l;
#else
LARGE_INTEGER l;
lpGetFileSizeEx(hFile, &l);
return l.QuadPart;
#endif
}

void LargeFile::SeekToBegin()
{
#ifndef _CAN_UPLOAD_CASE_MAX2GB_
unsigned int l = 0;
SetFilePointer(hFile, l, NULL, FILE_BEGIN);
return;
#else
LARGE_INTEGER l;
l.QuadPart = 0;
//bool b =
lpSetFilePointerEx(hFile, l, NULL, FILE_BEGIN);
#endif

}

void LargeFile::SeekToEnd()
{
#ifndef _CAN_UPLOAD_CASE_MAX2GB_
unsigned int l = 0;
SetFilePointer(hFile, l, NULL, FILE_END);
return ;
#else
LARGE_INTEGER l;
l.QuadPart = 0;
//bool b=
lpSetFilePointerEx(hFile, l, NULL, FILE_END);
#endif
}

void LargeFile::Seek(UINT64 dis)
{
#ifndef _CAN_UPLOAD_CASE_MAX2GB_
unsigned int l;
l = (UINT)dis;
SetFilePointer(hFile, l, NULL, FILE_BEGIN);
return;
#else
LARGE_INTEGER l;
l.QuadPart = dis;
lpSetFilePointerEx(hFile, l, NULL, FILE_BEGIN);
#endif
}

UINT LargeFile::Read(char* buffer, UINT len)
{
DWORD r;
ReadFile(hFile, buffer, len, &r, NULL);
return r;
}


void LargeFile::Write(char*buffer, UINT len)
{
DWORD r;
//bool b =
WriteFile(hFile, buffer, len, &r, NULL);
}

 

출처: https://use1348.tistory.com/20 [유용한 정보:티스토리]