forked from WizardMac/librdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writeEx.c
69 lines (53 loc) · 1.85 KB
/
writeEx.c
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <math.h>
#include <fcntl.h>
#include <rdata.h>
static ssize_t write_data(const void *bytes, size_t len, void *ctx) {
int fd = *(int *)ctx;
return write(fd, bytes, len);
}
void writeRData() {
int row_count = 3;
int fd = open("example.RData", O_CREAT | O_WRONLY, 0644);
rdata_writer_t *writer = rdata_writer_init(&write_data, RDATA_WORKSPACE);
rdata_column_t *col1 = rdata_add_column(writer, "column1", RDATA_TYPE_REAL);
rdata_column_t *col2 = rdata_add_column(writer, "column2", RDATA_TYPE_STRING);
rdata_begin_file(writer, &fd);
rdata_begin_table(writer, "my_table");
rdata_begin_column(writer, col1, row_count);
rdata_append_real_value(writer, 0.0);
rdata_append_real_value(writer, 100.0);
rdata_append_real_value(writer, NAN);
rdata_end_column(writer, col1);
rdata_begin_column(writer, col2, row_count);
rdata_append_string_value(writer, "hello");
rdata_append_string_value(writer, "goodbye");
rdata_append_string_value(writer, NULL);
rdata_end_column(writer, col2);
rdata_end_table(writer, row_count, "My data set");
rdata_end_file(writer);
close(fd);
}
void writeRDS() {
int row_count = 3;
int fd = open("example.rds", O_CREAT | O_WRONLY, 0644);
rdata_writer_t *writer = rdata_writer_init(&write_data, RDATA_SINGLE_OBJECT);
rdata_column_t *col = rdata_add_column(writer, "column1", RDATA_TYPE_REAL);
rdata_begin_file(writer, &fd);
rdata_begin_column(writer, col, row_count);
rdata_append_real_value(writer, 42.0);
rdata_append_real_value(writer, -7.0);
rdata_append_real_value(writer, NAN);
rdata_end_column(writer, col);
rdata_end_file(writer);
close(fd);
}
int main() {
writeRData();
writeRDS();
printf("Done\n");
exit(0);
}