Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old March 6th, 2004, 04:08 AM
Registered User
 
Join Date: Jul 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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


*******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);
}

Reply With Quote
  #2 (permalink)  
Old March 10th, 2004, 12:24 PM
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

That is true. I had this problem too.
And there is an other problem,
let's say you have the file1 and copy the contents to file2.
The size will be, 11 and 12 bytes respectively.
Now, if you copy file2 to file1, the sizes will be 12 and 13 bytes and so on.....
Maybe garbage in memory!
I'm working on it!

Reply With Quote
  #3 (permalink)  
Old March 10th, 2004, 02:20 PM
Authorized User
 
Join Date: Feb 2004
Posts: 76
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Here's a suggestion: whenever you get a result you don't understand, use printf() to troubleshoot.

For example, use something like
Code:
printf("Debug: inChar = <%02X>\n", inChar);
after the line that reads a byte from the file.

If this doesn't tell you something (that is, if you don't understand,
what it tells you) get back to us.


printf() is your friend.


Regards,

Dave
Reply With Quote
  #4 (permalink)  
Old March 13th, 2004, 02:41 PM
Registered User
 
Join Date: Mar 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

As Dave said, printf() is better solution.

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

    while (!feof(inFp)) {
        inChar = getc(inFp);
        printf("\nThe %i item of file1 : <%02X>\n",i, inChar);

        putc(inChar, outFp);
        printf("The %i item of file2 : <%02X>\n",i, inChar);*/
        i++;
    }

But the problem exists. If you trace the chars with this source code, by making file1 empty, you will see, by copying to file2, that there is an <FFFF> Try it!

Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
error in .vbs FTP routine invoked from VB 6.0 dgr7 VBScript 0 February 7th, 2007 05:29 PM
sub Routine problem NitinJoshi General .NET 1 January 5th, 2005 04:10 AM
Macro to copy data from one file to a 2nd file chadpodsednik Excel VBA 1 October 29th, 2004 10:40 AM
How to copy file? eapsokha Classic ASP Databases 2 November 14th, 2003 11:44 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.