binary File Q.?

hanann's Avatar, Join Date: Oct 2009
Light Poster
if i want to read abinary file and copy it to another one using fwrite and fread

that i will passing an integer and arry of string then open file in it,s mode

my Qu. is how can i do it to open any kind of file like vedio or exe file can i copy and store them
using C languge and what this idea need ?

the second Q. is should i know the size of byets that i want to copy

help me with your answer
0
ungalnanban's Avatar, Join Date: Feb 2010
Go4Expert Member
you can read the text file using fread and you can write the content using fwrite.

the read and write system calls are allowed only for files. it will never read a special files. like directory , socket, FIFO,

if you want to read a directory you can use opendir.

you can specify the buffer size in read and write. if you cant to read single character or single line at a file.




0
virxen's Avatar, Join Date: Nov 2009
Pro contributor
is this what you are looking for?

Code:
#include <stdio.h>


/* Main program */

int main (int argc, char * argv[])
{
   FILE * fin, * fout;
   unsigned char buffer[1000];
   size_t count;

   fin = fopen(argv[1], "rb");
   if (fin == NULL)
      return 1;

   fout = fopen(argv[2], "wb");
   if (fout == NULL)
      return 2;
      
   while (!feof(fin)) {
      count = fread(buffer, 1, 1000, fin);
      fwrite(buffer, 1, count, fout);
   }

   fclose(fin);
   fclose(fout);

   return 0;
}