/* test_binres.c */ #include #include #include "binres.h" #include "mdtypes.h" #define WRITE_LEN 3 #define NELEM(x) (sizeof(x)/sizeof(x[0])) int main(int argc, char **argv) { MD_Dvec vecbuf[] = { { 1, 2, 3}, { 2, 3, 4}, { 3, 4, 5}, { 4, 5, 6}, { 5, 6, 7}, { 6, 7, 8}, { 7, 8, 9}, { 8, 9, 0} }; MD_Dvec inbuf[2]; Binres b; MD_Dvec *dvec; const char *binfile; int len, num, max, i, retval; if (argc != 2) { fprintf(stderr, "usage: %s binfile\n", argv[0]); exit(1); } binfile = argv[1]; /* here are the vectors to write */ printf("The original vectors:\n"); for (i = 0; i < NELEM(vecbuf); i++) { printf("%g %g %g\n", vecbuf[i].x, vecbuf[i].y, vecbuf[i].z); } /* writing binary restart file */ binres_init(&b); binres_open(&b, binfile, BINRES_WRITE, MD_DVEC); dvec = vecbuf; num = 0; len = WRITE_LEN; max = NELEM(vecbuf); printf("\nWriting binary restart file, %d values at a time.\n", len); do { if (len > max - num) len = max - num; retval = binres_write(&b, dvec, len, max); dvec += len; num += len; } while (retval > 0); binres_close(&b); printf("\nRead in the vectors, %d values at a time.\n", NELEM(inbuf)); binres_open(&b, binfile, BINRES_READ, MD_DVEC); do { retval = binres_read(&b, inbuf, NELEM(inbuf), &num, &max); printf("num = %d max = %d\n", num, max); for (i = 0; i < num; i++) { printf("%g %g %g\n", inbuf[i].x, inbuf[i].y, inbuf[i].z); } } while (retval > 0); binres_close(&b); binres_destroy(&b); return 0; }