Reputation: 4510
I am having some trouble reading a jpg file and saving it. I want to implement a file sharing system between a client and server and I am unable to even read a jpg and save it on the same process. Here is what I have so far
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);
}
When it tries to read the file, it tells me that fileSize is 0.
Upvotes: 0
Views: 5225
Reputation: 41017
fstat() is identical to stat(), except that the file to be stat-ed is specified by the file descriptor fd.
You are passing FILE *
, fstat
expects an int
Upvotes: 3