Thread: copy file

  1. #1
    Unregistered
    Guest

    copy file

    beginner C

    How can i copy an existent file content to a new file that I have to create

  2. #2
    Mayor of Awesometown Govtcheez's Avatar
    Join Date
    Aug 2001
    Location
    MI
    Posts
    8,823
    K&R page 171.

    k thx bye.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main ( void )
    {
      size_t len;
      FILE *in, *out;
      char buf[BUFSIZ];
    
      if ( ( in = fopen ( "datain.txt", "rb" ) ) != NULL ) {
        if ( ( out = fopen ( "dataout.txt", "wb" ) ) != NULL ) {
          do {
            len = fread ( buf, 1, sizeof buf, in );
            if ( len != 0 )
              fwrite ( buf, 1, len, out );
            /*
            ** This would be a good place to check for errors.
            */
          } while ( len == sizeof buf );
          fclose ( out );
        }
        else
          perror ( "File open failure: output" );
        fclose ( in );
      }
      else
        perror ( "File open failure: input" );
    
      return EXIT_SUCCESS;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM