我在读取和保存 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/