Beispiel #1
0
/*
 * Note: amessage__pack and amessage__unpack will allocate memory from Heap,
 * please remember to free them.
 */
void pack_to_file(int argc, char *argv[])
{
	AMessage msg = AMESSAGE__INIT; // AMessage
	void *buf;                     // Buffer to store serialized data
	unsigned len;                  // Length of serialized data
	FILE *fp = NULL;

	msg.a = atoi(argv[1]);
	if (argc == 3) {
		msg.has_b = 1;
		msg.b = atoi(argv[2]);
	}
	len = amessage__get_packed_size(&msg);

	buf = malloc(len);
	amessage__pack(&msg, buf);

	fprintf(stderr, "Writing %d serialized bytes\n", len); // See the length of message

	if (NULL == (fp = fopen(FILE_NAME, "w"))) {
		fprintf(stderr, "fopen failed");
		exit(-1);
	}
	fwrite(buf, len, 1, fp);
	fclose(fp);

	fwrite(buf, len, 1, stdout); // Write to stdout to allow direct command line piping
	free(buf); // Free the allocated serialized buffer
}
Beispiel #2
0
int main(int argc, const char *argv[])
{
	AMessage msg = AMESSAGE__INIT;
	void *buf;
	unsigned len;
	char test[1000] = {'\0'};

	msg.a = atoi(argv[1]);
	len = amessage__get_packed_size(&msg);
	
	buf = malloc(len);
	amessage__pack(&msg, buf);

	printf("%d\n", len);
	fwrite(buf, len, 1, stdout);
	
	free(buf);
	return 0;	
}