[Experience] a question about File Replication

Source: Internet
Author: User

 

# Include <conio. h>
# Include <stdio. h>
# Include <stdlib. h>
Int main (void)
...{
File * In, * out;
Clrscr ();
If (in = fopen ("D:/h1.txt", "R") = NULL)
...{
Printf ("can't open infile! ");
Exit (0 );
}
If (out = fopen ("D:/h2.txt", "W") = NULL)
...{
Printf ("can't open OUTFILE! ");
Exit (0 );
}
While (! Feof (in ))
Putc (GETC (in), OUT );


Fclose (in );
Fclose (out );
Return 0;

}

The code in the book should be reasonable...
However, in practice, whether copying a binary or text file, a Y-like character is always displayed after the new file .. Always one character more than the original file...

While (! Feof (in ))
Putc (GETC (in), OUT );

However, the above two lines of code are copied until the end of the file .. The file Terminator is not copied to the new file.

Cause: the GETC function returns the next character of the input stream and Adds 1 to the file location indicator. When the indicator points to the last character, GETC actually returns EOF, therefore, the EOF will be written in.

In fact, it will be better to process both text files and binary files,

While (! Feof (in ))
{
Int CH = GETC (in );
If (Ch! = EOF)
Putc (CH, out );
Else
Break;
}
However, if it is written in this way, you can only process text files.

Char CH = GETC (in );
While (Ch! = EOF ){
Putc (CH, out );
Ch = GETC (in );
} Because the binary file must use feof to determine the end Of the file ;;

Summary:

(1) byte reading

Normally, GETC reads the file stream as an unsigned char, expands to an integer, and returns
Back. In other words, GETC takes a byte from the file stream and adds 24 zeros to become an integer smaller than 256,
Then return.

Int C;
While (C = fgetc (RFP ))! =-1) //-1 is EOF
Fputc (C, WFP );

Although C in fputc is an integer, the integer is 24 bits in height before fputc writes it to the file stream.
Removed, so fgetc and putc can be used together to implement File Replication. So far, C has been defined
Char is still feasible, but we will see below that defining C as int is to determine whether the segment file is complete correctly.

(2) determine the end of the file.

Most people think that there is an EOF in the file, which is used to indicate the end Of the file. However, this is actually incorrect.
There is no file terminator in the data contained in the file. For GETC, if it cannot be read from the file,
Returns an integer-1, which is the so-called EOF. The returned EOF is nothing more than two situations.
After reading; second, an error occurred while reading the file.

Note: Normally, the returned integers are less than 256, that is, 0x0 ~ 0xff. but cannot read the returned results
It is 0xffffffff. However, if you use fputc to write 0 xffffffff to the file, the 24-bit high will be blocked, and the write Will
It is 0xff. // lixforalpha. Please note this

(3) will 0xff confuse us?

No, provided that the C that receives the returned value is defined as int according to the prototype.

If the next read character is 0xff

Int C;
C = fgetc (RFP); // C = 0x000000ff;
If (C! =-1) // Of course,-1 is 0 xffffffff
Fputc (WFP); // Oh, oxff copied successfully.

The character 0xff, which is not EOF.

(4) Define c as char

Assume that the next read character is 0xff.

Char C;
C = fgetc (RFP); // The value of fgetc (RFP) is 0x000000ff, Which is secretly reduced to bytes. c = 0xff
If (C! =-1) // compare the character with the integer? C is expanded to 0 xffffffff by signed, oh,
If the condition is true, File Replication exits early.

While (C = fgetc (RFP ))! = EOF! Unexpected suspension.

(5) Define c as unsigned char;

When you read the end of the file and return EOF, that is,-1,

Unsigned char C;
C = fgetc (RFP); // The value of fgetc (RFP) is EOF, that is,-1, that is, 0 xffffffff, in bytes, c = 0xff
If (C! =-1) // C is extended to 0x000000ff and never returns 0 xffffffff

Therefore, although 0xff can be correctly copied this time, the end of the file cannot be determined. In fact, when C is uchar,
C! =-1 is always true. A high-quality compiler, for example, GCC will point this point during compilation.

(6) Why feof?
File * FP;
FP points to a very complex data structure. feof uses the mark in this structure to determine whether the file ends.
If the file is read using fgetc, when the last character is read, The EOF mark in FP will not be opened.
If you use feof to determine whether the file is closed, the conclusion is displayed.

When fgetc returns-1, we still cannot be sure that the file has ended, because it may be a read error! Then we
Feof and ferror are required.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.