Thread: copying binary file with streams instead of byte by byte, C only

  1. #1
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87

    copying binary file with streams instead of byte by byte, C only

    Hi. I study C.

    I am working on a better version of a program that will copy a given binary file using streams instead of byte by byte.

    Is this possible with C? Or can I work with binary files using streams in C++ only?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Well, in C you have the functions fread and fwrite which can use data of an arbitrary size, whether you have an array or a structure.

  3. #3
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    Is better an array or a structure for that?

    I have currently two versions (both are inspired by from c - Copying Binary Files - Stack Overflow)

    Could you please help me with it? I do not have a laptop and I can only use online compiler that cannot work with files.

    version1:

    Code:
    #include <stdint.h>
    #include <stdio.h>
    
    int main()
    {
    
        FILE *exein, *exeout;
    
        exein = fopen("filein.ttt", "rb");    
        if (exein == NULL)
        {
            /* handle error */
            perror("file open for reading");
            exit(EXIT_FAILURE);
        }
        exeout = fopen("fileout.ttt", "wb");
        if (exeout == NULL)
        {
            /* handle error */
            perror("file open for writing");
            exit(EXIT_FAILURE);
        }
    
        size_t n, m;
        unsigned char buff[8192];
        do
        {
            n = fread(buff, 1, sizeof buff, exein);
            if (n) m = fwrite(buff, 1, n, exeout);
            else   m = 0;
        } while ((n > 0) && (n == m));
        if (m) perror("copy");
    
        if (fclose(exeout)) perror("close output file");
        if (fclose(exein)) perror("close input file");
    
        return 0;
    }
    version2:

    Code:
    #include <stdint.h>
    #include <stdio.h>
    #include <ctype.h>
    #define BUFSIZE 1024
    
    int main(int argc, char **argv)
    {
        char mybuf[BUFSIZE] = { 0 }, *p = NULL;
        FILE *ifd = NULL, *ofd = NULL;
        ifp = fopen( argv[1], “r” );
        ofp = fopen( argv[2], “w” );
        assert(ifp!=NULL);
        assert(ofp!=NULL);
        while ( ( n = fread( mybuf, sizeof(char), BUFSIZE ,ifd) ) > 0 )
        {
            fwrite(mybuf, sizeof(char),BUFSIZE,ofd);
        }
        fclose(ifd);
        fclose(ofd);
        return 0;
    }
    here is a manual with structures
    http://www.codingunit.com/c-tutorial-binary-file-io

    and here is one using arrays (below there)
    http://www.tutorialspoint.com/ansi_c...with_files.htm

    That is soo difficult!
    Last edited by nerio; 01-20-2016 at 04:46 PM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,616
    Is better an array or a structure for that?
    It depends on the data involved. Arrays are homogeneous though. So, if you have a lot of ints written in binary, an array is better. Most binary files are complex. Like you can design a format made up of separate fwrite calls, writing down different things.
    I do not have a laptop and I can only use online compiler that cannot work with files.
    Frankly, that is silly. Why do you want to program then?

    You can make files with this online IDE: Free Online IDE and Terminal

  5. #5
    Registered User
    Join Date
    Sep 2010
    Location
    Europe
    Posts
    87
    I intend to write a program that XORs two given files, until the length of the shorter one. As an exercise I was advised here to write a different program first that will copy a given file.

    Frankly, that is silly. Why do you want to program then?
    Well, in the future I will have a laptop.

    Usually I use either one of

    https://www.remoteinterview.io/online-c-compiler
    https://ideone.com

    I will check out the one you posted. Thank you.

  6. #6
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    version 2 contains error - it ignores n, so if input file is let's say 10 bytes long
    output file will be 1024 bytes long...
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is there one extra byte when copying file?
    By xbfish in forum C Programming
    Replies: 3
    Last Post: 07-21-2011, 03:15 PM
  2. File Comparision byte by byte
    By anusha2489 in forum C Programming
    Replies: 12
    Last Post: 05-16-2011, 06:58 AM
  3. reading files byte by byte
    By cpsc in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2011, 03:54 PM
  4. Turning Byte into Binary
    By Lang in forum C Programming
    Replies: 8
    Last Post: 09-06-2008, 10:31 AM
  5. Binary File - Byte order / endian
    By chico1st in forum C Programming
    Replies: 27
    Last Post: 08-22-2007, 07:13 PM