Skip to content

Instantly share code, notes, and snippets.

@otaks
Created May 2, 2016 08:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save otaks/768c6f9bac62ca975e9ae0a5c911e5c9 to your computer and use it in GitHub Desktop.
Save otaks/768c6f9bac62ca975e9ae0a5c911e5c9 to your computer and use it in GitHub Desktop.
GetFileSize
#include <stdio.h>
typedef int bool;
bool true = 1;
bool false = 0;
/**
* ファイルサイズをバイト単位で取得。
*
* @param pName 対象ファイル名(IN)
* @param pos バイト数(OUT)
* @return true:成功、false:失敗
*/
bool GetFileSize(const char *pName, fpos_t *pos){
FILE *fp; /* ファイルポインタ */
int iRet; /* 戻り値 */
fp = fopen(pName, "rb");
if(!fp){
return false;
}
/* ファイル位置表示子をファイル末尾に移動する */
iRet = fseek(fp, 0L, SEEK_END);
if(iRet != 0){
return false;
}
/* ファイル位置表示子の位置を取得 */
iRet = fgetpos(fp, pos);
if(iRet != 0){
return false;
}
/* ファイル位置表示子をファイル先頭に移動する */
iRet = fseek(fp, 0L, SEEK_SET);
if(iRet != 0){
return false;
}
fclose(fp);
return true;
}
int main(){
bool bRet;
fpos_t pos;
bRet = GetFileSize("C:\\bbb.txt", &pos);
printf("size = %ld\n", (long)pos);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment