GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-Mar-2004, 02:11
janet janet is offline
New Member
 
Join Date: Mar 2004
Posts: 1
janet is on a distinguished road

File copy routine error!


I am writing a program that can duplicate a binary file.
However, the new file will hav extra byte (0xFF) at the end of new file. For example, original file (123.bin) contains 11 Bytes, the generated file (abc.bin) is 12 Bytes, because an extra 0xFF appended at the end of it. How to get rid of it?

123.bin: 6162631198F5BB77660061
abc.bin: 6162631198F5BB77660061FF

CPP / C++ / C Code:
*******source code***************
#include <stdio.h>
#include <stdlib.h>

FILE *inFp;
FILE *outFp;

void main() {
	char inFilename[12];
	char outFilename[12];

	int inChar;

	printf("What is the name of the file you want to back up? ");
	gets(inFilename);

	printf("What is the name of the file ");
	printf("you want to copy %s to? ", inFilename);
	gets(outFilename);

	if ((inFp = fopen(inFilename, "rb")) ==NULL) {
		printf("\n\n*** %s does not exist ***\n", inFilename);
		exit(0);
	}

	if ((outFp = fopen(outFilename, "wb")) == NULL) {
		printf("\n\n*** Error opening %s ***\n", outFilename);
		exit(0);
	}

	printf("\nCopying...\n");

	while (!feof(inFp)) {
		inChar = getc(inFp);
		putc(inChar, outFp);
	}

	printf("\nThe files are copied.\n");

	fclose(inFp);
	fclose(outFp);
}
Last edited by dsmith : 06-Mar-2004 at 07:44. Reason: Please use [c] & [/c] to enclose C code for highlighting
  #2  
Old 06-Mar-2004, 07:53
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Hi Janet. Welcome to GIDForums. I think that your program is outputing EOF. If I remember correctly feof will only be true if the file pointer is sitting on feof. So if you look at your while loop:


CPP / C++ / C Code:
        while (!feof(inFp)) {
		inChar = getc(inFp);
		putc(inChar, outFp);
	}

Your program is getting the end of file, storing it into inChar, writing it out and then checking for the end of file. I would fix this by adding a quick if statement like so:

CPP / C++ / C Code:
while( !feof(inFp) ){
    if( (inChar = getc(inFp) ) != EOF)
          putc(inChar, outFp);
}

See if that fixes it.

Cheers,
d
  #3  
Old 06-Mar-2004, 12:03
WaltP's Avatar
WaltP WaltP is online now
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all
Quote:
Originally Posted by dsmith
Hi Janet. Welcome to GIDForums. I think that your program is outputing EOF. If I remember correctly feof will only be true if the file pointer is sitting on feof. So if you look at your while loop:


CPP / C++ / C Code:
        while (!feof(inFp)) {
		inChar = getc(inFp);
		putc(inChar, outFp);
	}

Your program is getting the end of file, storing it into inChar, writing it out and then checking for the end of file. I would fix this by adding a quick if statement like so:

CPP / C++ / C Code:
while( !feof(inFp) ){
    if( (inChar = getc(inFp) ) != EOF)
          putc(inChar, outFp);
}

See if that fixes it.

Cheers,
d
Actually, you should not used feof() to control a loop. Instead change your loop to:
CPP / C++ / C Code:
while ((inChar = getc(inFp)) != EOF)
{
    putc(inChar, outFp);
}

Also, do NOT use gets(). It has a tendancy to make your program explode. Change the gets(inFilename) to
CPP / C++ / C Code:
fgets(inFilename, 12, stdin);
i = strlen(inFilename)-1;
if (inFilename[i] == '\n') inFilename[i] = '\0'; //  remove the trailing newline

The problem with gets() is if you enter ABCDEFGHIJKLMNOPQRSTUVWXYZ.FILETYPE as the filename, the 1st 12 characters will be loaded into the variable, the characters after those 12 get loaded into the bytes following -- destroying other variable contents and possibly writing to space not available to the program. The program then crashes. So never use gets(). It's not worth the headaches.
 
 

Recent GIDBlogStupid Management Policies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mozilla Thunderbird dsmith Computer Software Forum - Linux 9 01-Mar-2005 12:56
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
[script] Handling Error 404 JdS PHP Code Library 0 19-Nov-2003 09:22
[script] E-mail webmaster error page BobbyDouglas PHP Code Library 0 19-Aug-2003 21:10

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 02:31.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.