File Operations-copy file1 to file2]

Source: Internet
Author: User

# Include <stdio. h>

Void main (INT argc, char * argv [])

{

Char ch;

If (argc! = 3)

Printf ("\ n \ nfformat: mycopy <source> <DESTIN> ");

Else

{

File * Source, * DESTIN;

Source = fopen (argv [1], "R ");

DESTIN = fopen (argv [2], "W ");

If (Source = NULL)

Printf ("can not open the source file % s \ n", argv [1]);

Else if (DESTIN = NULL)

Printf ("can not open the DESTIN file % s \ n", argv [2]);

Else

{

While (CH = fgetc (source ))! = EOF)

Fputc (CH, DESTIN );

}

Fclose (source );

Fclose (DESTIN );

}

}

 

1. Use of command line parameters: Main (INT argc, char * argv [])


2. File end judgment

The while (! Feof (source) fputc (fgetc (source), DESTIN); there is always an extra garbled code at the end;

The problem is:

Feof (FP) has two return values: if the file ends, the feof (FP) value of the function is 1; otherwise, it is 0.

EOF is the end mark of a file.In a text fileThe data is stored in the form of the characters ASC ⅱ code value. The range of ASC ⅱ code is 0 to 255, and-1 is not possible,Therefore, you can useEOF indicates the end of a file.

When the data is stored in a binary file, the value-1 is displayed. Therefore, the EOFEnd flag of a binary file. To solve this problem,Asci c provides a feof function to determine whether a file is terminated. The feof function can be used to determine both binary files and text files.

The "feof ()" function in the "c" language is totally different from the "EOF ()" function in the database. The "EOF ()" function in the database reads the position of the current pointer. The "feof ()" function in the "c" language returns the last "read operation content ". After many years of mixing "location and content", the concept is similar.

So what is the difference between location and content? For example, some people say that "You go to the last train box" is the location. If we say "please keep walking backward and touch the rails to the end", this is the content. That is to say, "One more section" will be judged by the content ". This is completely dependent on "while (! When the feof (FP) {...} file is copied, the target document will always be "more" than the source document.

The "C" file cannot be fully dependent on "while (! Feof (FP) {...} "judgment.

Difference from EOF

In stdio. H, you can see the following definitions:

# Define EOF (-1)

# DEFINE _ ioeof 0x0010

# Define feof (_ Stream)(_ Stream)-> _ flag & _ ioeof)

Int C;

While (! Feof (FP ))

{

C = fgetc (FP );

Printf ("% x \ n", C );

}

  One more outputFF, because after reading the last character, FP-> flag is still not set to _ ioeofTherefore, feof () still does not detect the end of the file. Feof () cannot detect the end of a file until fgetc () is called again. In this way, one more-1 (FF) is output ).

The correct statement should be:

Int C;

C = fgetc (FP );

While (! Feof (FP ))

{

Printf ("% x \ n", C );

C = fgetc (FP );

}

Can feof () be replaced by EOF? No. When fgetc returns-1,There are two cases: Reading the end of a file or reading an error. Therefore, we cannot be sure that the file has ended., Because it may be a read error! In this case, we need feof ().


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.