我在读取 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);
}
当它尝试读取文件时,它告诉我 fileSize 为 0。