copy file

This is a discussion on copy file within the C Programming forums, part of the General Programming Boards category; beginner C How can i copy an existent file content to a new file that I have to create...

  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,825
    K&R page 171.

    k thx bye.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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, 01: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

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