#navi(contents-page-name): No such page: ファイル

ファイルのサイズを取得する・stat

stat関数によりファイルの情報を取得することができます。
本資料はstat関数を使用し、引数で指定したファイルのファイルサイズ(容量)を表示するC言語サンプルコードです。

c-top.html is not found or not readable.

関連記事

ファイルサイズ取得のC言語サンプルコード

&ref(): File not found: "fsize.c" at page "ファイル/ファイルのサイズを取得する・stat"; (改行コードLF)

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

void usage(char *cmd)
{
	fprintf(stderr, "Usage: %s <filename>\n", cmd);
}

int main(int argc, char *argv[])
{
	struct stat st;
	if (argc != 2) {
		usage(argv[0]);
		return 1;
	}
	
	if (stat(argv[1], &st) != 0) {
		fprintf(stderr, "%s is not found.\n", argv[1]);
		return 1;
	}
	
	printf("File size : %d\n", st.st_size);
	
	return 0;
}

ファイルサイズ取得の実行結果

コンパイルして実行した結果を以下に記します。

$ gcc fsize.c -o fsize
$ ./fsize fsize.c 
File size : 421

$ ls -l fsize.c
-rw-r--r-- 1 sakura sakura 421  7月 26 14:13 fsize.c

以上、ファイルサイズ(容量)の取得方法でした。

c-btm.html is not found or not readable.

トップ   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS