-2

I have to use fwrite() instead of fputc(), but I can't understand how it works. I have tried to use it but it writes in binary. This is the code:

while ((c = fgetc(f1)) != EOF) 
{  
    if (fwrite(&f3, sizeof(f3), 1, f3) == EOF) 
    {
        printf("valore iniziale di errno = %d\n",errno);
        perror("errore:");
        exit(1);        
    }
}

f1 is the file where some text is written and f3 is the file where I want to write the content of f1 but I see only binary text.

Raman
  • 2,735
  • 1
  • 26
  • 46
Jean
  • 31
  • 2
  • 3
  • 1
    If you want a string representation of something written you have to first represent it in a string, then write it. `snprintf()` is your friend. – Yunnosch Jan 12 '18 at 16:05
  • 1
    You probably have, or can find, the source code for `fwrite`. If you mean you don't understand how fwrite usage works, there are plenty of online tutorials for that. – Rob Jan 12 '18 at 16:06
  • @Rob i know but i can't understand the parameters that fwrite require. So i don't know if the parameters that i have put are right or not – Jean Jan 12 '18 at 16:09
  • 2
    It is really basic knowledge. The C tag wiki points to a [list of books](https://stackoverflow.com/questions/562303/the-definitive-c-book-guide-and-list) among which [The C Book](http://publications.gbdirect.co.uk/c_book/) that contains a chapter about formatted (printf) and unformatted (fwrite) IO. It is available on line so you should read it. Feel free to come back here to ask more precise questions. – Serge Ballesta Jan 12 '18 at 16:18
  • 2
    You tried using it but it writes in binary? From the man page for `fwrite`: "fread, fwrite - **binary** stream input/output". The documentation is your friend. –  Jan 12 '18 at 16:20
  • It looks like you're trying to read the contents of one file and write it to another just with a single `fwrite`. That won't work. `fwrite` can't copy the contents of a file by referencing the file pointer from `fwrite`. You need to read the manual page for `fwrite` carefully. It will write out the contents of a data buffer. A file stream pointer is NOT a data buffer pointer containing file contents. – lurker Jan 12 '18 at 16:45

1 Answers1

4

How fwrite "works"


You can start by looking at the fwrite manual. It's signature is:

size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

So, let's break down each argument:

  1. ptr is the source location of your data (where you data is).
  2. size is the size of each element of data. fwrite writes in chunks of size bytes each time it loops.
  3. nmemb is the number of loops fwrite will perform.
  4. stream is the file descriptor (FILE*) of your output file.

So, with that in mind, imagine this:

// our dear string
char *str = "#SO";

// open a bogus file to write
FILE *fp = fopen(foo, "wb");

// call fwrite and store returned value
size_t ret = fwrite(str, 3, 1, fp);

This code will do the following (other than not compiling): Starting at location pointed by str, it will write 3 bytes in the file pointed by fp 1 time. However, if you did it like this:

size_t ret = fwrite(str, 1, 3, fp);

That would tell fwrite to write 1 byte to the file for every iteration it does, and it would do 3 iterations.

In your case, you read a character from f1 and want to write it to f3. Therefore, you should have something like this:

ret = fwrite(&c, sizeof(c), 1, f3);

That will read the binary character from f1 and copy it to f3.

Note that this might not be (and probably is not) the way fwrite works internally. What I presented here is the mental model I use to remember its arguments. If you actually read fwrite source code, you may find a very different implementation.

Example


So, a simple program to copy from f1 to f2 is:

#include <stdio.h>

int main(void)
{
    char c;
    FILE *f1 = fopen("foo.txt", "rb");
    FILE *f2 = fopen("bar.txt", "wb");
    size_t ret;

    while((ret = fread(&c, sizeof(c), 1, f1))) 
        ret = fwrite(&c, sizeof(c), 1, f2);


    fclose(f1);
    fclose(f2);
    return 0;
}
Enzo Ferber
  • 3,029
  • 1
  • 14
  • 24
  • You might note that `fwrite()` will not return EOF under most practical circumstances. To return EOF, you'd have to attempt to write an enormous amount of data — `(size_t)-1` bytes worth — and it would have to succeed. – Jonathan Leffler Jan 12 '18 at 16:44