blob: 0a7f48168bd5dad463da91a40c6061296cd60d4f (
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
28
29
30
31
32
33
34
35
36
37
38
39
|
#include <stc/cstr.h>
#include <stc/algo/coroutine.h>
#include <errno.h>
// Read file line by line using coroutines:
struct file_nextline {
const char* filename;
int cco_state;
FILE* fp;
cstr line;
};
bool file_nextline(struct file_nextline* U)
{
cco_begin(U)
U->fp = fopen(U->filename, "r");
U->line = cstr_init();
while (cstr_getline(&U->line, U->fp))
cco_yield(true);
cco_final: // this label is required.
printf("finish\n");
cstr_drop(&U->line);
fclose(U->fp);
cco_end(false);
}
int main(void)
{
struct file_nextline it = {__FILE__};
int n = 0;
while (file_nextline(&it))
{
printf("%3d %s\n", ++n, cstr_str(&it.line));
//if (n == 10) cco_stop(&it);
}
}
|