Skip to content

Instantly share code, notes, and snippets.

@7shi
Created March 7, 2014 12:28
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 7shi/9410583 to your computer and use it in GitHub Desktop.
Save 7shi/9410583 to your computer and use it in GitHub Desktop.
[C++]スパースファイルの実験
#include <stdio.h>
int main() {
// 10バイトのファイルを作成
char a[10] = {0};
FILE *f = fopen("test", "wb");
fwrite(a, 1, sizeof(a), f);
fclose(f);
f = fopen("test", "r+b");
// 100バイト目にシーク(範囲外だがOK)
fseek(f, 100, SEEK_SET);
// 読み込み(失敗)
printf("%d\n", fread(a, 1, sizeof(a), f));
// 書き込み(成功) → ファイルサイズが110バイトになる
printf("%d\n", fwrite(a, 1, sizeof(a), f));
fclose(f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment