Get File Size

À©µµ¿ìÁî¿¡¼­ ÆÄÀÏ »çÀÌÁ ±¸ÇØ º¸ÀÚ.

< stat¸¦ ÀÌ¿ëÇÑ ¹æ¹ý >

#include <sys/types.h>

#include <sys/stat.h>

__int64 GetFileSize64( const char * szFileName)

{

    struct __stat64 fileStat;

    int err = _stat64( szFileName, &fileStat );

    if(err != 0)

        return 0;

 

    return fileStat.st_size;

}

 

< GetFileAttributesEx¸¦ ÀÌ¿ëÇÑ ¹æ¹ý >

__int64 GetFileSize(const char *path)

{

    WIN32_FILE_ATTRIBUTE_DATA attr = { 0 };

    if( !::GetFileAttributesEx(path, GetFileExInfoStandard, &attr))

        return 0;

 

    __int64 fileSize = 0;

    fileSize = attr.nFileSizeHigh;

    fileSize <<= 32;

    fileSize += attr.nFileSizeLow;

    return fileSize;

}

 

< ftellÀ» »ç¿ëÇÑ ¹æ¹ý >

long GetFileSize( const std::string path )

{

    FILE *pFile = NULL;

 

    fopen_s( &pFile, path.c_str(), "rb" );

    fseek( pFile, 0, SEEK_END );

 

    long size = ftell( pFile );

 

    //ÆÄÀÏÀ» Àб⸦ ¿øÇϸé ÆÄÀÏ Æ÷ÀÎÅ͸¦ ½ÃÀÛÁ¡À¸·Î µ¹¸°´Ù

    // rewind( pFile );

 

    fclose( pFile );

    return size;

}

ÆÄÀÏ »çÀÌÁ ¾ò¾î¿Â ÈÄ ¹Ù·Î ÆÄÀÏ Àб⸦ ÇÒ·Á¸é rewind¸¦ ½ÇÇàÇÑ´Ù.

 

< FindFirstFileÀ» ÀÌ¿ëÇÏ´Â ¹æ¹ý >

ULONGLONG GetFileSizeEx( const std::string& strPath )

   WIN32_FIND_DATA FindData = { 0 };

   //gets a file search handle

   HANDLE hFirstFile = FindFirstFile( strPath.c_str(), &FindData ); 

 

   //if the handle is valid

   if( hFirstFile != INVALID_HANDLE_VALUE )

   {

      //closes the file search handle

      FindClose( hFirstFile );

 

      ULONGLONG FileSize = FindData.nFileSizeHigh;

      FileSize <<= sizeof( FindData.nFileSizeHigh ) * 8;

      FileSize |= FindData.nFileSizeLow;

 

      return FileSize;

   }

 

   return 0; // File not found

}

 

< CreateFileÀ» ÀÌ¿ëÇÏ´Â ¹æ¹ý >

__int64 GetFileSizeEx(const std::string& name)

{

    HANDLE hFile = CreateFile(name.c_str(), GENERIC_READ,

        FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING,

        FILE_ATTRIBUTE_NORMAL, NULL);

    if (hFile==INVALID_HANDLE_VALUE)

        return -1;

 

    LARGE_INTEGER size;

    if (!GetFileSizeEx(hFile, &size))

    {

        CloseHandle(hFile);

        return -1;

    }

 

    CloseHandle(hFile);

    return size.QuadPart;

}

Ãß°¡·Î Æú´õ »ý¼º API¿¡ ´ëÇؼ­ ¾Ë¾Æº¸ÀÚ.

< Æú´õ »ý¼º API >

#include <Dbghelp.h>

#pragma comment( lib, "Dbghelp.lib" )

 

BOOL bRes = MakeSureDirectoryPathExists("d:\\work\\test\\test\\test\\makedir\\");

Æú´õ ¸¶Áö¸·¿¡ "\\"À» ºÙ¿©¾ß ¸¶Áö¸· Æú´õ ±îÁö ¸¸µé¾î Áø´Ù.

 

Ãß°¡·Î ´ë¿ë·® ÆÄÀÏ Àбâ, ¾²±â ¿¹Á¦ÀÌ´Ù.

< ´ë¿ë·® ÆÄÀÏ Àбâ >

#define out

 

struct FileMemInfo

{

    unsigned char* pRead;

    long size;

};

 

bool ReadFile(std::string path, out FileMemInfo& info)

{

    FILE *pFile = NULL;

 

    errno_t err = fopen_s( &pFile, path.c_str(), "rb" );

    if(err != 0)

        return false;

 

    fseek( pFile, 0, SEEK_END );

    long size = ftell( pFile );   

    rewind(pFile);

 

    unsigned char *buff = new unsigned char [size];

 

    long nReadSize = 0, n = 0;

    while((n = fread(buff + nReadSize, 1, size - nReadSize, pFile)) > 0)

    {

        nReadSize += n;

    }

 

    fclose(pFile);

 

    info.pRead = buff;

    info.size = size;

    return true;

}

 

< ´ë¿ë·® ÆÄÀÏ ¾²±â >

bool WriteFile(std::string path, const FileMemInfo& info)

{

    FILE *pFile = NULL;

 

    errno_t err = fopen_s( &pFile, path.c_str(), "wb" );

    if(err != 0)

        return false;

 

    unsigned char* buff = info.pRead;

    long count = info.size;

 

    long nWriteSize = 0, n = 0;

    while((n = fwrite(buff + nWriteSize, 1, count - nWriteSize, pFile)) > 0)

    {

        nWriteSize += n;

    }

 

    fclose(pFile);

 

    return true;

}