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.
- Standard input. The default standard input is the system's general input device, usually the keyboard
- Standard output.
- 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