Programming of standard library in UNIX

Source: Internet
Author: User

# Include <stdio. h>
File * fopen (const char * finename, const char * type );
File * freopen (const char * filename, const char * type, file * stream );
Int fclose (File * stream );
Int remove (const char * filename );
Int Rename (const char * oldname, const char * newname );

I. fopen FunctionsThe fopen function is used to open a file. The call format is:
File * fopen (char * filename, * type );
The first form parameter in the fopen () function represents the file name, which can contain two parts: path and file name. For example:
"B: test. dat"
"C: // TC // test. dat"
Note: It is incorrect to write the path "C:/TC/test. dat. The fopen function is used to open a file. Generally, it is called in the form of: file pointer name = fopen (file name, file method ),
The "file pointer name" must be a pointer variable described as file type, and the "file name" is the file name of the opened file.
"File Usage" refers to the file type and operation requirements. The "file name" is a String constant or a string array. For example:
File * FP;
Fp = ("file a", "R ");
It means to open file a in the current directory, only allow "read" operations, and direct FP to the file.
Another example:
File * fphzk
Fphzk = ("C: // hzk16'," rb ")
It means to open the file hzk16 under the root directory of drive C, which is a binary file and only allows read operations in binary mode.
The first of the two Backslash "//" represents the escape character, and the second represents the root directory. There are 12 methods to use files. Their symbols and meanings are given below.
The second form parameter indicates the type of the file to open. For more information about file types, see the following table. Table File Operation Type
When there are too many threads, there are too many threads, too many threads.
Character meaning
── ─
"R" Open Text File Read-Only
"W" Create a text file and write only
"A" addition. If the file does not exist, create
"R +" open a text file to read/write
"W +" Create a text file read/write
"A +" open or create a file supplement
"B" binary file (can be used with each of the above items)
"T" file (default)
When there are too many threads, there are too many threads, too many threads.

File Usage
"RT" only opens a text file and only supports reading data.
"WT" only writes to open or create a text file, only data can be written
Append "at" to open a text file and write data at the end of the file.
"Rb" is used to open a binary file and only read data.
"WB" only writes to open or create a binary file, only data can be written
Append "AB" to open a binary file and write data at the end of the file.
"RT +" reads and writes to open a text file, allowing reading and writing
"WT +" reads and writes to open or create a text file, allowing reading and writing
"At +" to open a text file, allow reading, or append data to the end of the file
"RB +" reads and writes a binary file, allowing reading and writing
"WB +" to open or create a binary file and allow reading and writing
"AB +" to open a binary file, allow reading, or append data to the end of the file. The following describes how to use the file:
1. The file is comprised of R, W, A, T, B, and +. The meanings of each character are as follows:
R (read): Read
W (write): Write
A (append): append
T (text): Text File, can be omitted without writing
B (banary): Binary File
+: Read and Write 2. When you use "R" to open a file, the file must already exist and can only be read from the file. 3. A file opened with "W" can only be written to this file. If the opened file does not exist, the file is created with the specified file name. If the opened file already exists, delete the file and create a new file. 4. to append new information to an existing file, you can only open the file in "A" mode. However, this file must exist at this time; otherwise, an error will occur. 5. If an error occurs when opening a file, fopen returns a null pointer value. In the program, this information can be used to determine whether to complete the file opening and handle it accordingly. Therefore, the following sections are commonly used to open files:
If (FP = fopen ("C: // hzk16", "rb") = NULL)
{
Printf ("/nerror on Open C: // hzk16 file! ");
Getch ();
Exit (1 );
}
The meaning of this program is that if the returned pointer is null, it indicates that the hzk16 file under the root directory of the C drive cannot be opened, the message "error on Open C:/hzk16file!" is displayed !", The next line of getch () is used to input a character from the keyboard, but is not displayed on the screen. Here, the role of this row is to wait. The program continues to be executed only when you press one key from the keyboard. Therefore, you can use this wait time to read the error message. Press the key and execute exit (1) to exit the program. 6. when reading a text file into the memory, you need to convert the ASCII code into a binary code. When writing the file into a disk as text, you also need to convert the binary code into an ascii code, therefore, it takes a lot of time to read and write text files. This conversion does not exist for reading/writing binary files.
Ii. freopen Functions
Freopen replaces the file stream. It first closes the original file stream, and then opens a new file stream in fopen mode, any operations on the original file stream are automatically converted to operations on the new file stream. If the operation succeeds, the file pointer pointing to the new file stream is returned. If the operation fails, the return value is null:
Path: file name, used to store the custom file name of input and output.
Mode: the mode in which the file is opened. The mode is the same as that in fopen (such as R-read-only and W-write.
Stream: a file, usually using a standard stream file.
Return Value: success. A pointer to the file specified by path is returned. If no pointer is returned, null is returned. (Generally, the return value is not used)
Function: Implement redirection to direct a predefined standard stream file to a file specified by path. Standard stream files refer to stdin, stdout, and stderr. Stdin is the standard input stream, the default is the keyboard; stdout is the standard output stream, the default is the screen; stderr is the standard error stream, generally set the screen as the default. By calling freopen, you can modify the default value of the standard stream file to achieve redirection.
Iii. fclose Function
Close an open file stream
Iv. Remove functions
The remove () function returns 0 if the file specified by fname (file name) is successfully removed (). If an error occurs, a non-zero value is returned.
V. Rename Function
It can be used to rename files or folders.
The principle is to modify the structure attributes of files and folders in the system.

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.