1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

binary File Q.?

Discussion in 'C' started by hanann, Feb 18, 2010.

  1. hanann

    hanann New Member

    Joined:
    Oct 2, 2009
    Messages:
    9
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    CS student
    Location:
    palestine
    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
     
  2. ungalnanban

    ungalnanban New Member

    Joined:
    Feb 19, 2010
    Messages:
    45
    Likes Received:
    2
    Trophy Points:
    0
    Location:
    Chennai
    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.




    :cuss::cuss:
     
  3. virxen

    virxen New Member

    Joined:
    Nov 24, 2009
    Messages:
    387
    Likes Received:
    90
    Trophy Points:
    0
    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;
    }
    
     

Share This Page