File Copying - Random chars at the end

This is a discussion on File Copying - Random chars at the end within the C Programming forums, part of the General Programming Boards category; Hi, well I was trying to write a simple, small program that copy files. The code is this: Code: #include ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    58

    File Copying - Random chars at the end

    Hi, well I was trying to write a simple, small program that copy files.

    The code is this:

    Code:
    #include <stdio.h>
    
    int main(int argc, char** argv)
    {
        if (argc < 3)
        {
            printf("Not enought arguments supplied.");
            exit(1);
        }
    
        FILE* f1;
        FILE* f2;
    
        f1=fopen(argv[1],"r");
    
        if(f1==NULL)
        {
            printf("Source file does not exist.");
            exit(1);
        }
    
        f2=fopen(argv[2],"w");
    
        while(!feof(f1))
        {
            putc(getc(f1),f2);
        }
    
        fclose(f1);
        fclose(f2);
    
        return 0;
    
    
    }
    But when I copy the files some random chars get added at the end of the destination file.

    Anyone knows what's wrong?

    Oh, another question: What's the library for exit()?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Check the FAQ on the problem with using feof() to control a loop. The basic idea is that you'll call feof(), and EOF will not be reached, so it will return false. You enter the loop, call getc(), and it returns EOF. Now feof() will return true, but that's too late because you're printing the representation of EOF into the file.

    Code:
    while(!feof(f1))
    {
        putc(getc(f1),f2);
    }
    Change to something like this:

    Code:
    int c;
    ...
    while((c = getc(f1)) != EOF)
    {
        putc(c,f2);
    }
    exit() is from stdlib.h btw.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Yes, it's because you're using feof() to control the while loop.
    This is explained in the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Don't use feof() to control loops see the FAQ

    consider,
    Code:
    int c;
    
    while((c = getc(f1)) != EOF)
        putc(c, f2);
    edit: too slow

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    58
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem building Quake source
    By Silvercord in forum Game Programming
    Replies: 16
    Last Post: 07-11-2010, 09:13 AM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM

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