Created
January 6, 2015 14:46
-
-
Save jalcine/d031ad0adaa2bf5ad430 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "learnuv.h" | |
#define BUF_SIZE 37 | |
static const char *filename = __MAGIC_FILE__; | |
int main() { | |
int r = 0; | |
uv_loop_t *loop = uv_default_loop(); | |
/* 1. Open file */ | |
uv_fs_t open_req; | |
r = uv_fs_open(loop, &open_req, filename, O_RDONLY, S_IRUSR, NULL); | |
if (r < 0) CHECK(r, "uv_fs_open"); | |
/* 2. Create buffer and initialize it to turn it into a a uv_buf_t which adds length field */ | |
char buf[BUF_SIZE]; | |
uv_buf_t fs_buf = uv_buf_init(buf, sizeof(buf)); | |
/* 3. Use the file descriptor (the .result of the open_req) to read from the file into the buffer */ | |
uv_fs_t read_req; | |
r = uv_fs_read(loop, &read_req, open_req.result, &fs_buf, sizeof(buf), 0, NULL); | |
if (r < 0) CHECK(r, "uv_fs_read"); | |
/* 4. Report the contents of the buffer */ | |
log_report("%s", buf); | |
log_info("%s", buf); | |
/* 5. Close the file descriptor (`open_req.result`) */ | |
uv_fs_t close_req; | |
r = uv_fs_close(loop, &close_req, open_req.result, NULL); | |
if (r < 0) CHECK(r, "uv_fs_close"); | |
/* always clean up your requests when you no longer need them to free unused memory */ | |
uv_fs_req_cleanup(&open_req); | |
uv_fs_req_cleanup(&read_req); | |
uv_fs_req_cleanup(&close_req); | |
uv_run(loop, UV_RUN_DEFAULT); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment