-1

This is my file copying code:

void main()
{
    char c;
    FILE *fptr1, *fptr2;

    fptr1=fopen("abc.docx","r");
    fptr2=fopen("paste_here.docx","w");

    c=fgetc(fptr1);
    while (c!=EOF)
    {
        putc(c,fptr2);
        c=fgetc(fptr1);
    }
    fclose(fptr1);
    fclose(fptr2);
}

I expected the contents to get copied but whenever I try to open paste_here.docx file after running this, it says that the file can't be opened as there are problems with the contents.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 4
    `fgetc()` returns `int` by intention. – alk Jul 08 '19 at 06:04
  • 2
    Is the output file the correct size? You should have defined `char c;` as `int c;` and a data value of `0xFF` may have caused premature ending of the loop. – Weather Vane Jul 08 '19 at 06:05
  • 1
    Use `fread`and `fwrite` instead, reading/writing char by char is very inefficient. Youz shoulkd also indent your code (just like the samples in your C text book) – Jabberwocky Jul 08 '19 at 06:05
  • 2
    It doesn't say if you are using Miscosoft compiler, but if you are, you must specify the file r/w mode when copying a file like this, binary mode `"rb"` and `"wb"` to prevent data that looks like (but is not) a line-end character being converted. – Weather Vane Jul 08 '19 at 06:09
  • 1
    See also [`while ((c = getc(fp)) != EOF)` loop won't stop executing](https://stackoverflow.com/questions/13694394/) — you can also run foul of premature EOF if the byte 0xFF is present in the file being copied unless you use the `b` flag. – Jonathan Leffler Jul 08 '19 at 06:25
  • 1
    @Jabberwocky, a character oriented read relies on an underlying read-buffer and is quite efficient. For example `fgetc()` actually reads from a `BUFSIZ` buffer (previously `_IO_BUFSIZ` in gcc source) which is 8192 bytes on Linux, and 512 bytes on Windows. So a char-by-char read is no less efficient than a read with `fgets` using an 8192 byte buffer. (see: `_IO_BUFSIZ` changed to `BUFSIZ` [glibc commit 9964a14579e5eef9](https://sourceware.org/git/?p=glibc.git;a=commit;h=9964a14579e5eef925aaa82facc4980f627802fe)) – David C. Rankin Jul 08 '19 at 07:01
  • @DavidC.Rankin but even with a buffer (512 on Windows is tiny BTW) there is an overhead, but actually I didn't mesure. – Jabberwocky Jul 08 '19 at 07:35
  • `char c;` -> `int c;` should do the job. – Jabberwocky Jul 08 '19 at 07:38
  • I agree, for a zippier copy, a `memmap` or `sendfile` (Linux) can easily best the large block copy in user-space. `sendfile` even manages to do most of the copy in kernel-space. – David C. Rankin Jul 08 '19 at 07:41

1 Answers1

0

Above code works well with GCC compiler. however, this should not be dependent on the compiler.

Here are the two types of file data

1 - text file easy to read and write.

2 - binary files having complex and encrypted information.

Suggestion :

For a text file, we would suggest you use fprintf() and fscanf() function for reading and writing operation.

For a binary file, we would suggest you use fread() and fwrite() funtion for reading and writing operation.

dragon
  • 132
  • 6
  • 2
    The fact that "binary files having complex and encrypted information" is not relevant to the question. The OP just wants to copy a file regardless of it it's a binary or a text or whatever file. – Jabberwocky Jul 08 '19 at 07:37
  • I'm very new to all this so I don't really understand what a binary file is. My initial code worked only on .txt files. But when I opened a .docx file in rb and wb mode, the same code worked. THANKS a lot. It would be really great if you could please explain to me why this is happening. Also can I do the same for .xls files? – Kashvi Gandhi Jul 08 '19 at 17:48
  • 1
    @KashviGandhi refer to below link for better understanding between 'r' and rb' https://stackoverflow.com/questions/2174889/whats-the-differences-between-r-and-rb-in-fopen for 'w' and 'wb' https://stackoverflow.com/questions/43777913/the-difference-in-file-access-mode-w-and-wb hope you will the idea from here. if you think the above post works for you, pls upvote this answer. – dragon Feb 21 '20 at 06:44