summaryrefslogtreecommitdiffhomepage
path: root/examples/read.c
blob: 2324d1dfe2dbec1b9dc5ec769b8927b21395a0fc (plain)
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
#include <errno.h>
#include <stc/cstr.h>
#include <stc/cvec.h>

using_cvec_str();

cvec_str read_file(const char* name) {
    cvec_str vec = cvec_str_init();
    for (FILE* f = fopen(name, "r"); f; fclose(f), f=NULL) {
        cstr line = cstr_init();
        while (cstr_getline(&line, f))
            cvec_str_emplace_back(&vec, line.str);
        cstr_del(&line);
    }
    return vec;
}

int main() {
    cvec_str vec = read_file("read.c");
    if (errno) printf("errno: %d\n", errno);

    int n = 0;
    c_foreach (i, cvec_str, vec)
        printf("%5d: %s\n", ++n, i.ref->str);

    cvec_str_del(&vec);
}