Thread: File Copying function

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    24

    File Copying function

    Hi everyone,
    I wrote a programme to copy the contents of one file into another and I agree that I wrote some useless and extra code. The programme works fine except that it adds an extra ÿ at the end of the file that it copies contents to.

    Here's the code:
    Code:
    #include <stdio.h>
    #include <unistd.h>
    
    void fcp(FILE *fp, FILE *sfp);
    
    int main (int argc, char *argv[])
    {
    	FILE *fp;
    	FILE *sfp;
    	char c, h;
    	int x;
    
    	if (argc < 2)
    	{
    		printf("Proper Usage:\n./D20Exc5 <FILENAME1> <FILENAME2>\n");
    		return 0;
    	}
    	if ((fp = fopen(argv[1], "r")) == NULL)
    	{
    		printf("ERROR Opening file %s.\n", argv[1]);
    		return 1;
    	}
    	if ((sfp = fopen(argv[2], "w+")) == NULL)
    	{
    		printf("ERROR Opening file %s.\n", argv[2]);
    		return 1;
    	}
    	fcp(fp, sfp);
    	for (x = 0; !feof(fp); x++)
    	{
    		fseek(fp, x, SEEK_SET);
    		fseek(sfp,x, SEEK_SET);
    		c = fgetc(fp);
    		h = fgetc(sfp);
    		if (c != h)
    		{
    			puts("Failed Copying Files...");
    			puts("Trying Again...");
    			puts("Press Ctrl-C to stop at anytime.");
    			execl("/home/rhodium/C/Day20/D20Exc5", "fcp", 
    					argv[1], argv[2]);
    		}
    	}
    	fclose(fp);
    	fclose(sfp);
    	return 0;
    }
    
    void fcp(FILE *fp, FILE *sfp)
    {
    	char c;
    	while(!feof(fp))
    	{
    		c = fgetc(fp);
    		putc(c, sfp);
    	}
    }
    Could someone tell me why that extra character is at the end of the file and how to remove it?

    Thank you
    Thanx
    Rhodium

  2. #2
    Registered User pedro_velho's Avatar
    Join Date
    Jul 2003
    Posts
    7

    Lightbulb traditional

    Probably you are reading an invalid character before living the while loop try this:

    Code:
                    while(!feof(fp))
    	{
    		c = fgetc(fp);
    		
    putc(c, sfp);
    	}

  3. #3
    Registered User pedro_velho's Avatar
    Join Date
    Jul 2003
    Posts
    7
    sorry for the last reply the anwser is here, (i have some dumbness while typing tab key)?:



    Code:
    //your way
    	while(!feof(fp))
    	{
    		c = fgetc(fp);
    		putc(c, sfp);
    	}
    
    //alternate way
    	while(1)
    	{
    		c = fgetc(fp);
                                    if(feof(fp) != 0) break;
    		putc(c, sfp);
    	}

  4. #4
    Registered User
    Join Date
    Dec 2002
    Posts
    24
    Thanx, Pedro: It is solved.
    Could you explain to me what the problem was!?
    I mean, in my old way, shouldn't the programme first check for end of file and then copy a character? I don't really understand the difference between your code and mine except for the position of feof() and I don't understand how it can make a difference. So could you please explain it to me?

    Thanx
    Thanx
    Rhodium

  5. #5
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by pedro_velho
    sorry for the last reply the anwser is here, (i have some dumbness while typing tab key)?:
    Use spaces instead
    Code:
    //alternate way
        while(1)
        {
            c = fgetc(fp);
            if(feof(fp) != 0) break;
            putc(c, sfp);
        }
    Are you sure the last byte is output in this case? It looks to me that on last byte this code:
    1) reads the last byte which sets EOF
    2) check EOF and exit skipping the putc()

    How about:
    Code:
        do
        {
            c = fgetc(fp);
            putc(c, sfp);
        }  while(feof(fp));
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  6. #6
    Registered User
    Join Date
    Dec 2002
    Posts
    24
    Well, as I said I don't really understand the difference that Pedro's code makes but it works perfectly.
    Originally posted by WaltP
    Code:
    do
        {
            c = fgetc(fp);
            putc(c, sfp);
        }  while(feof(fp));
    It seems to me like this code will only copy the first character because after it does, feof(fp) is not TRUE and the loop quits. Correct me if I'm totally off.
    Thanx
    Rhodium

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Read the FAQ to understand your problem. Then, rewrite your code correctly:

    Code:
    int c;
    while ((c = fgetc(fp)) != EOF)
        fputc(c, sfp);
    /* Now use feof() and/or ferror() to determine stream status */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM