-5

Im trying to copy contents of one image file to another. But somehow my code creates an empty image. Here is the code

void main() {
  FILE* i = fopen("index.jpg", "r");
  int size = 0;

  int index = 0;
  char buff[1024];
  FILE* j = fopen("abc.jpg", "w+");
  while (!feof(i)) {
    fread(buff, 1, 1024, i);
    fwrite(buff, 1, 1024, j);
    index += 1024;
    fseek(i, index, SEEK_SET);
    seek(j, index, SEEK_SET);
  }
  fclose(i);
  fclose(j);
  FILE* picture = fopen("abc.jpg", "r");
  fseek(picture, 0, SEEK_END);
  size = ftell(picture);
  fseek(picture, 0, SEEK_SET);
  printf("Total Picture size: %i\n", size);
}
Mark Benningfield
  • 2,800
  • 9
  • 31
  • 31
cranberry
  • 55
  • 7
  • 4
    Careful..., JPEG files are *binary* files, and depending on platform opening a file in non-binary mode (like you do) might cause translation of certain byte-sequences. Also please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude Mar 10 '18 at 10:00
  • @Someprogrammerdude so changing the mode to rb and wb while keeping rest of the code same will work? – cranberry Mar 10 '18 at 10:01
  • More related to your problem, why are you seeking in the file? The read and write calls will update the file pointer correctly. And will the code even build? What is `seek`? – Some programmer dude Mar 10 '18 at 10:02
  • Compiles without errors. – cranberry Mar 10 '18 at 10:03
  • Please clean up this trainwreck of code formatting. A binary file must be opened with the "b" mode. [while(feof)](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) is always wrong. `void main` is non-standard, avoid. – n. m. could be an AI Mar 10 '18 at 10:03
  • 1
    You don't do any error checking. Fix that first, I'm halfway sure you will find out where things go wrong. – Ulrich Eckhardt Mar 10 '18 at 10:05
  • When creating a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve), please make sure it's both *minimal* and *complete* and *verifiable*. Your code isn't complete nor verifiable. Copy-paste the actual code you have instead. – Some programmer dude Mar 10 '18 at 10:06

2 Answers2

1

Your code almost works.

Seems that the seek part is not necessary since the offset is automatically increased. Please make sure to write only the number of read bytes otherwise the length of the destination image may become larger than the source image, however you may be able to open it.

int main()
{
    FILE *i = fopen("index.jpg", "r");
    int size=0;

    int index=0;
    char buff[1024];

    FILE *j = fopen("abc.jpg", "w+");
    while(!feof(i))
    {
        int len = fread(buff, 1,1024,i);
        fwrite(buff,1,len,j);
        //index += 1024;
        //fseek( i,index, SEEK_SET );
        //fseek( j,index, SEEK_SET );
    }
    fclose(i);
    fclose(j);

    FILE *picture=fopen("abc.jpg","r");
    fseek(picture, 0, SEEK_END);
    size = ftell(picture);
    fseek(picture, 0, SEEK_SET);

    printf("Total Picture size: %i\n",size);
    return 0;
}
Werner
  • 281
  • 1
  • 12
0

Why don't you just make it simple:

#include<stdio.h>
#include<stdlib.h>

int main()
{
    FILE *fs,*ft;
    int ch;

    fs=fopen("your/file/path","rb");
    if(fs==NULL)
    {
        puts("Unable to open source!");
        exit(1);
    }
    ft=fopen("new/file/path","wb");
    if(ft==NULL)
    {
        puts("Unable to copy!");
        fclose(fs);
        exit(2);
    }

    while(1)
    {
        ch=fgetc(fs);
        if(ch==EOF) break;
        fputc(ch,ft);
    }
    fclose(fs);
    fclose(ft);
    return 0;
}
Mohit Negi
  • 42
  • 2
  • 9