int main(int argc, char *argv[]) { test_msg2_t msg; unsigned char *buf; int fd, bufsize, size, wsize; if (argc != 2){ fprintf(stderr, "Usage: %s out.bin >out.txt\n", argv[0]); return -1; } msg2Rand(&msg); borMsgSetHeader(&msg, test_msg2_t_schema); buf = NULL; bufsize = 0; size = borMsgEncode(&msg, test_msg2_t_schema, &buf, &bufsize); fd = open(argv[1], O_WRONLY | O_CREAT, 0644); if (fd < 0){ fprintf(stderr, "Error: Could not open `%s'\n", argv[1]); return -1; } wsize = 0; while (wsize < size) wsize += write(fd, buf + wsize, size - wsize); close(fd); msg2Print(&msg, stdout); borMsgFree(&msg, test_msg2_t_schema); BOR_FREE(buf); return 0; }
int main(int argc, char *argv[]) { test_msg2_t msg; unsigned char *buf; int fsize, bufsize, size, wsize; FILE *fin; if (argc != 2){ fprintf(stderr, "Usage: %s in.bin >out\n", argv[0]); return -1; } fin = fopen(argv[1], "rb"); if (fin == NULL){ fprintf(stderr, "Error: Could not open `%s'\n", argv[1]); return -1; } fseek(fin, 0, SEEK_END); fsize = ftell(fin); fseek(fin, 0, SEEK_SET); buf = malloc(fsize + 1); fread(buf, fsize, 1, fin); fclose(fin); borMsgDecode(buf, fsize, &msg, test_msg2_t_schema); BOR_FREE(buf); buf = NULL; bufsize = 0; size = borMsgEncode(&msg, test_msg2_t_schema, &buf, &bufsize); wsize = 0; while (wsize < size) wsize += write(1, buf + wsize, size - wsize); borMsgFree(&msg, test_msg2_t_schema); BOR_FREE(buf); return 0; }