C:如何从文件中读取一个jpg并保存?

标签 c file download jpeg

我在读取和保存 jpg 文件时遇到了一些问题。我想在客户端和服务器之间实现一个文件共享系统,我什至无法读取 jpg 并将其保存在同一进程中。这是我目前所拥有的

int main(int argc, const char * argv[])
{
    char *buffer;
    FILE *picture;
    FILE *newPicture;
    struct stat st;
    long fileSize = 0;


    picture = fopen("PATH/root/game-of-thrones-poster.jpg", "rb");
    fstat(picture, &st);
    fileSize = st.st_size;
    if(fileSize > 0) {
        buffer = malloc(fileSize);

        if(read(picture, buffer, fileSize) < 0) {
            printf("Error reading file");
        }
        fclose(picture);

        newPicture = fopen("PATH/root/new.jpg", "wb");
        write(newPicture, buffer, fileSize);
    }
    free(buffer);
}

当它尝试读取文件时,它告诉我文件大小为 0。

最佳答案

fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.

您正在传递 FILE *fstat 需要一个 int

关于C:如何从文件中读取一个jpg并保存?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16371355/

相关文章:

c - 用C将文件数据直接写入zip文件

C语言: When you dereference a "pointer to a pointer to int",应该返回什么?

c - 标准C库中缓冲区大小的特殊宏是什么?

Java获取具有正确编码的url

javascript - 在 Node JS 中下载文件

node.js - Node-Webkit 文件下载进度

c++ - 在 C 或 C++ 中打印调用堆栈

c - 是否可以在文本文件中定义指向字符的指针?

在 C++11 中调用 C 函数时,我可以将 std::vector 作为 FILE* 潜入吗?

ios - 应用程序强制退出时如何恢复下载?