Skip to content

Instantly share code, notes, and snippets.

@kumagi
Created October 3, 2016 13:54
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 kumagi/1f68facaeb139ef83e837715ea1f95d2 to your computer and use it in GitHub Desktop.
Save kumagi/1f68facaeb139ef83e837715ea1f95d2 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/time.h>
#include <assert.h>
int main(void) {
size_t a = 2;
int b = 10;
{ // read
FILE* f = fopen("data.log", "rb+");
if (f) {
size_t ra;
int rb;
int cnt = 0;
for (;;) {
size_t a_nread = fread(&ra, 1, sizeof(ra), f);
if (a_nread < sizeof(size_t)) { break; }
//printf("nread: %zd\n", a_nread);
//printf("a: %zd ra: %zd\n", a, ra);
assert(a == ra);
size_t b_nread = fread(&rb, 1, sizeof(rb), f);
if (b_nread < sizeof(rb)) {
printf("read not meat\n");
break;
}
assert(b == rb);
//printf("nread: %zd\n", b_nread);
//printf("b: %d rb: %d\n", b, rb);
++cnt;
}
printf("%d data read\n", cnt);
}
}
printf("validation success\n");
{ // append
FILE* f = fopen("data.log", "wb");
size_t i;
for (i = 0; i < 1000; ++i) {
fwrite(&a, sizeof(a), 1, f);
fflush(f);
usleep(1000);
fwrite(&b, sizeof(b), 1, f);
fflush(f);
}
fclose(f);
}
printf("write finished\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment