zoukankan      html  css  js  c++  java
  • 用VC的read函数读取文件,读取的数据比实际文件大小要少的问题

    今天用VC下编译libevent的http-server示例,却发现用浏览器怎么也打不开网页,跟踪下来,发现运行到 evbuffer_add_file 函数就阻塞了

    起初怀疑是 libevent的evbuffer_add_file函数实现有Bug,所以自己写了个简单的实现来替换

        //evbuffer_add_file(evb, fd, 0, st.st_size);
        char *xbuf = (char *)malloc(st.st_size);
        int n = read(fd, xbuf, st.st_size);
        if (n < (int)st.st_size) {
            n = read(fd, xbuf+n, st.st_size-n);
        }
        evbuffer_add(evb, xbuf, n);
        free(xbuf);
        close(fd);

    现在可以打开网页了,但文件网页显示不完整,原来是read函数的问题,在这里read到的长度要比文件大小要小,即使反复read也不成功

    在网上百度了windows下read的问题找到了答案,原来windows下的_open函数默认不是按二进制格式打开文件的,需要在open时增加O_BINARY标志

    找到问题所在就好办了,修改http-server.cpp文件中open函数的参数后,一切正常

    //if ((fd = open(whole_path, O_RDONLY)) < 0) {
    if ((fd = open(whole_path, O_RDONLY|O_BINARY)) < 0) {

    附一个windows下使用open/read函数读取文件的示例,以备后用

    #include <sys/stat.h>
    #include <io.h>
    #include <fcntl.h>
    #include <malloc.h>
    
    #ifndef S_ISDIR
    #define S_ISDIR(x) (((x) & S_IFMT) == S_IFDIR)
    #endif
    
    #ifdef WIN32
    #define stat  _stat
    #define fstat _fstat
    #define open  _open
    #define close _close
    #endif
    
    int main(int argc, char **argv)
    {
        int fd, n;
        char *buf;
        struct stat st;
    
        if ((fd = open("a.bin", O_RDONLY|O_BINARY)) < 0) {
            perror("open");
            return 1;
        }
        if (fstat(fd, &st)<0) {
            perror("open");
            close(fd);
            return 1;
        }
        printf("filesize: %d
    ", st.st_size);
    
        buf = (char*)malloc(st.st_size);
        n = read(fd, buf, st.st_size);
        close(fd);
    
        if (n != (int)st.st_size) {
            printf("error: filesize = %d(bytes), read = %d(bytes)", (int)st.st_size, n);
        }
        return 0;
    }
  • 相关阅读:
    Oracle数据库——半期测验
    Oracle数据库——SQL高级查询
    mysql中整数类型后面的数字,是不是指定这个字段的长度?比如int(11),11代表11个字节吗?
    ehcache memcache redis 三大缓存男高音
    Java Redis Pipeline 使用示例
    游族网络:我们是怎么玩转千台以上游戏云服务器的
    java 在Excel中插入图片 POI实现
    解放运维的双手,谈自动化运维管理平台设计
    运维堡垒机
    查询相应的key
  • 原文地址:https://www.cnblogs.com/cppboy/p/3714974.html
Copyright © 2011-2022 走看看