summaryrefslogtreecommitdiffhomepage
path: root/examples/read.c
blob: b3ddcc0084f984be851a37dd4493212678b3619d (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();
    c_withfile (f, fopen(name, "r")) {
        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);
}