docs/Writerside/topics/streams.h.md

branch
docs/3.1
changeset 1141
a06a2d27c043
child 1142
9437530176bc
equal deleted inserted replaced
1140:88a9ee79c102 1141:a06a2d27c043
1 # streams.h
2
3 UCX provides some utilities for routine tasks.
4
5 The most useful utilities are the *stream copy* functions, which provide a simple way to copy all - or a
6 bounded amount of - data from one stream to another. Since the read/write functions of a UCX buffer are
7 fully compatible with stream read/write functions, you can easily transfer data from file or network streams to
8 a UCX buffer or vice-versa.
9
10 The following example shows, how easy it is to read the contents of a file into a buffer:
11 ```c
12 FILE *inputfile = fopen(infilename, "r");
13 if (inputfile) {
14 CxBuffer fbuf;
15 cxBufferInit(&fbuf, NULL, 4096, NULL, CX_BUFFER_AUTO_EXTEND);
16 cx_stream_copy(inputfile, &fbuf,
17 (cx_read_func) fread,
18 cxBufferWriteFunc);
19 fclose(inputfile);
20
21 // ... do something meaningful with the contents ...
22
23 cxBufferDestroy(&fbuf);
24 } else {
25 perror("Error opening input file");
26 if (fout != stdout) {
27 fclose(fout);
28 }
29 }
30 ```

mercurial