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?
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?
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.
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:
version2: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; }
here is a manual with structuresCode:#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; }
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.
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.Is better an array or a structure for that?
Frankly, that is silly. Why do you want to program then?I do not have a laptop and I can only use online compiler that cannot work with files.
You can make files with this online IDE: Free Online IDE and Terminal
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.
Well, in the future I will have a laptop.Frankly, that is silly. Why do you want to program then?
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.
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