前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >libuv 写文件

libuv 写文件

原创
作者头像
8菠萝
发布2022-11-14 14:25:42
3.3K0
发布2022-11-14 14:25:42
举报
文章被收录于专栏:菠萝上市没有
代码语言:c
复制
#include <stdio.h>
#include <spdlog/spdlog.h>
#include <string>
#include <uv.h>


typedef struct {
    std::string data;
    char buffer[10];

    uv_file open_file_no;
    size_t  wirte_index;

} write_context;


void on_write_cb(uv_fs_t *req)
{
    auto data = (write_context*)req->data;
    uv_fs_req_cleanup(req);
    if (req->result < 0)
    {
        spdlog::error("Write Error");
    }
    else if (req->result == 0)
    {
        spdlog::info("Close");
        uv_fs_close(uv_default_loop(), req, data->open_file_no, nullptr);
    }
    else
    {
        data->wirte_index = data->wirte_index + req->result;
        int size = sizeof(data->buffer);
        int index = data->wirte_index + size;
        if (index > data->data.length()){
            size = data->data.length() - data->wirte_index;
        }
        uv_buf_t uv_buf = uv_buf_init(data->buffer, size);
        strncpy(data->buffer, data->data.data() + data->wirte_index,  size);
        uv_fs_t write_fs;
        write_fs.data = data;
        uv_fs_write(uv_default_loop(), &write_fs, data->open_file_no, &uv_buf, 1, data->wirte_index, on_write_cb);
    }
}

void on_open_cb(uv_fs_t *req)
{
    uv_fs_req_cleanup(req);
    if (req->result <= 0)
    {
        spdlog::error("Open File fail:{}", uv_strerror(req->result));
    }
    else
    {
        auto data = (write_context*)req->data;
        data->open_file_no = req->result;
        int size = sizeof(data->buffer);
        int index = data->wirte_index + size;
        if (index > data->data.length()){
            size = data->data.length() - data->wirte_index;
        }
        uv_buf_t uv_buf = uv_buf_init(data->buffer, size);
        strncpy(data->buffer, data->data.data() + data->wirte_index,  size);
        uv_fs_t write_fs;
        write_fs.data = req->data;
        uv_fs_write(uv_default_loop(), &write_fs, data->open_file_no, &uv_buf, 1, 0, on_write_cb);
    }
}

int main()
{
    write_context context;
    context.data = "Winky’s lip quivered. Then she burst into tears, which spilled out of her great brown eyes and splashed down her front, just as they had done at the Quidditch World Cup.  ‘Oh, dear,’ said Hermione. She and Ron had followed Harry and Dobby to the end of the kitchen. ‘Winky, don’t cry, please don’t …’ But Winky cried harder than ever. Dobby, on the other hand, beamed up at Harry.  ‘Would Harry Potter like a cup of tea?’ he squeaked loudly, over Winky’s sobs.  ‘Er – yeah, OK,’ said Harry.  Instantly, about six house-elves came trotting up behind him, bearing a large silver tray laden with a teapot, cups for Harry, Ron and Hermione, a milk jug and a large plate of biscuits.  ‘Good service!’ Ron said, in an impressed voice. Hermione frowned at him, but the elves all looked delighted; they bowed very low and retreated.  ‘How long have you been here, Dobby?’ Harry asked, as Dobby handed round the tea.  ‘Only a week, Harry Potter, sir!’ said Dobby happily. ‘Dobby came to see Professor Dumbledore, sir. You see, sir, it is very difficult for a house-elf who has been dismissed to get a new position, sir, very difficult indeed";

    uv_fs_t open_fs;
    open_fs.data = (void*)&context;

    uv_fs_open(uv_default_loop(), &open_fs, "./text_write.txt", O_CREAT | O_WRONLY, 0, on_open_cb);
    return uv_run(uv_default_loop(), UV_RUN_DEFAULT);
}

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档