C Primer plus file input/output

Source: Internet
Author: User

file

A file is typically a named store on a disk. But for the operating system, the files are more complex. For example, a large file can be stored in a fragmented section, or it may contain additional data that enables the operating system to determine its file type.

C sees a file as a sequential sequence of bytes, where each byte can be read separately. ANSI C provides two views of the file: text view and binary view.

1° text view and binary view

The two file views required by ANSI are text view and binary view . In binary view, each byte in a file can be accessed by the program. In the text view, the contents of the program you see and the contents of the file may differ. Example: When reading a file using a text view, the local environment notation for the end of the line is mapped to the C view . Similarly, at the time of output, the line endings in C view are mapped to local environment notation. For example, an MS-dos text file uses a combination of carriage return and line break \ r \ n to represent the end of a line. The Macintosh text file uses only a carriage return \ r to represent the end of the line. The C program uses a \ n to represent the end of the line. So, if the C program processes an MS-DOS text file in text view, it will convert \ r \ n to read the file, it will convert \ n to \ n when writing to the file, and for the text view of the Macintosh text file, it will convert \ r to \ n when reading the file. It converts \ n to \ r when writing to a file.

Working with an MS-DOS text file does not have to be limited to just using text views. You can also use a binary view for such a file. If so, the program will see the \ r and \ n characters in the file, without any mappings occurring. MS-DOS distinguishes between text files and binary files , but C provides text and binary views. Typically, you use a text view for a text file, and a binary view for binary files. However, you can use either view to process a file, although working with a text view for binary files is bad.

ANSI C provides two modes of opening files: ① binary mode ② text mode

2° standard File

The C program automatically opens 3 files for you.

    1. Standard input. The default standard input is the system's general input device, usually the keyboard
    2. Standard output.
    3. Standard error output. The default standard output and standard error output is the general output device of the system, usually the monitor.

C Primer plus file input/output

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.