creating a copy of binary file

This is a discussion on creating a copy of binary file within the C Programming forums, part of the General Programming Boards category; hi... i have to create a copy of fits image(a binary file).i can read that original file using some other ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    creating a copy of binary file

    hi...
    i have to create a copy of fits image(a binary file).i can read that original file using some other program.i have the following code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<conio.h>
    #include <string.h>
    #include <fcntl.h>
    #include <errno.h>
    #include <limits.h>
    #include <errno.h>
    #include <time.h>
    
    int main()
    {
    
    //unsigned long filesize(FILE *fs,*ft);
    FILE *fs,*ft;
    char c;
    
    fs=fopen("alignment.raw","rb");
    
    ft=fopen("newalignment1.fit","wb");
    
    if(fs==NULL)
    
    perror("cannot open source file");
    else
      {
        do {
          c = getc (fs);
          printf("%c",c);
            fputc(c,ft);  
    	}
    //		while (c != EOF);
    		while (!feof(fs));
    
        fclose (fs);
    	fclose(ft);
    
      }
    
      getch();
      return 0;
    }
    now my problem is that the original file alignment.raw is of 2.07MB and the newalignment.raw which i suppose is the copy of original file is just 22.5KB.
    so can you plz tell me what is the problem with my code?
    is there any specified size limit for copy of any file?
    what is the difference between EOF and feof command?

    plz help...

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    if you do int c instead of char c, does the problem go away? Or use unsigned char?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,474
    Code:
    int c;
    while((c = fgetc (fs)) != EOF)
    {
        printf("%c",c);
        fputc(c,ft);  
    }
    EOF is return value of fgetc that indicates read error or end of file condition.
    in this case - nothing was read from the file - and nothing should be written

    feof() is flag that is set on the stream after failed read operation in case when the failure was due to the end-of-file condition
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Text file versus Binary file
    By sherwi in forum C Programming
    Replies: 6
    Last Post: 04-15-2006, 02:41 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21