-
Notifications
You must be signed in to change notification settings - Fork 5
/
write.cc
55 lines (45 loc) · 1.24 KB
/
write.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <fstream>
#include <string>
#include "messages.pb.h"
using namespace std;
int main(int argc, char *argv[]){
// Verify the version of the library
GOOGLE_PROTOBUF_VERIFY_VERSION;
int x, y, vel_x, vel_y;
FILE *fout;
string output;
if (argc != 2) {
cerr << "Usage: " << argv[0] << " file_to_store" << endl;
return -1;
}
swarm::Data data;
cout << "Enter x: ";
cin >> x;
data.set_x(x);
cout << "Enter y: ";
cin >> y;
data.set_y(y);
cout << "Enter vel_x: ";
cin >> vel_x;
data.set_vel_x(vel_x);
cout << "Enter vel_y: ";
cin >> vel_y;
data.set_vel_y(vel_y);
if (!data.SerializeToString(&output)) {
cerr << "Failed to write to the address book." << endl;
return -1;
}
fout = fopen(argv[1], "w");
fprintf(fout, "%s", output.c_str());
/*
* This isn't actually needed because the program dies after this but
* it is a good practise to do this because then if we extend this
* code to do some other thing so we should shutdown the protobuf
* library which frees up the memory
*/
google::protobuf::ShutdownProtobufLibrary();
return 0;
}