Node.js를가 libuv에 I/O (비동기 여부를) 처리하는 C 라이브러리를 사용합니다. 이렇게하면 이벤트 루프를 사용할 수 있습니다.
당신은 libuv이 무료 온라인 책/도입에 관심이있을 것 : http://nikhilm.github.com/uvbook/index.html
는 특히, reading/writing files에 전념 한 장있다.
int main(int argc, char **argv) {
// Open the file in write-only and execute the "on_open" callback when it's ready
uv_fs_open(uv_default_loop(), &open_req, argv[1], O_WRONLY, 0, on_open);
// Run the event loop.
uv_run(uv_default_loop());
return 0;
}
// on_open callback called when the file is opened
void on_open(uv_fs_t *req) {
if (req->result != -1) {
// Specify the on_write callback "on_write" as last argument
uv_fs_write(uv_default_loop(), &write_req, 1, buffer, req->result, -1, on_write);
}
else {
fprintf(stderr, "error opening file: %d\n", req->errorno);
}
// Don't forget to cleanup
uv_fs_req_cleanup(req);
}
void on_write(uv_fs_t *req) {
uv_fs_req_cleanup(req);
if (req->result < 0) {
fprintf(stderr, "Write error: %s\n", uv_strerror(uv_last_error(uv_default_loop())));
}
else {
// Close the handle once you're done with it
uv_fs_close(uv_default_loop(), &close_req, open_req.result, NULL);
}
}
node.js에 C++을 쓰려면이 책을 읽는 데 시간을 할애해야합니다. 그것은 가치.
std :: ofstream을 사용하시기 바랍니다. – Aleph