-------------------------------- Open, read, write, close's basics uses a concept: File Descriptor File Descriptor, INT, used to associate with files to be operated, there is usually three values: 0 Standard input 1 Standard Output 2 Standard error Open the file, is related to a file descriptor: int Open (const char * path, int OFLAGS); int open (const char * path); enter the way to open or want to create file path path, set the way to read and write OFLAGS (read, only write, Read and write), if you create a new file, you can also set the file permission Mode. Open successfully returned the int file descriptor read file size_t read (int Fildes, void * buf, size_t nbytes); enter the file descriptor Fildes from the parameter Open, read the NBYTES of NBYTES from Fildes, the return value is successful Read the number of bytes, if the failed returns -1 write file size_t write (int Fildes, const void * buf, size_t nbytes); and reading the NBYES by reading the NBYES by reading, to Fildes Document. Returns the number of bytes that actually written to turn off int close (int Fildes); Close the open file descriptor Filde is the most basic operation, you can already write a small program to practice: Practice description: a file copy program , Copy the content in a file to a new file
The following is some of the problems you should pay attention to, not all Open - pay attention to the read and write properties of the open file, do not write a read-only CD-ROM device - the second parameter oflags can also combine some settings, often using O_Trunc (deleted The content in the original file), O_Append (written behind the original file), o_creat, o_creat | o_excl (forced new file, if you already exist, Open failed) - If O_CREAT is used, it is in the third optional parameter. Indicates the permissions of the new file - Open the file descriptor to occupy kernel resources, so the file descriptor that can be opened at the same time in a process, usually 1024 Strlimit functions can change this value Close - For different devices, the behavior caused by Close Different: For a socket for Close, what is done is to disconnect the Socket network between two computers ... The following from Apue
READ - There are many situations that can make the actual number of bytes that are less than required reading bytes: • When reading a normal file, the file end is reached before reading the required byte number. For example, if there is 3 0 bytes before arriving at the end of the file, the read is required to read 1 0 0 bytes, and the READ returns 3 0, and when the next time, it will return 0 (file tail end). • When reading from terminal devices, you usually read up to one go to a line (Chapter 11 will introduce how to change this). • When read from the network, the buffer mechanism in the network may cause the return value less than the number of bytes required. • Some recordable devices, such as tape, return up to one record at a time. - The read operation starts from the current displacement amount of the file, and the bitmill increases the number of bytes read before successfully returns. A common reason for the Write- Write error is that the disk has been written, or the file length limit for a given process is exceeded - for normal files, the write operation starts from the current displacement of the file. If the O_Append selection is specified when the file is opened, the file bitmill is set at the current end of the file before each write operation. After a successful written, the file displacement increases the number of bytes that actually written. File Properties - File Permissions Set The Open files mentioned in the second parameter in the second parameter, if it is a new file, you can set a file in the third parameter if it is a new file. Permission. INT Open (const char * path, int rugs, mode_t mode); About O_RDOONLY (read) It is also the simplest setting. Note that these settings also take into account the rights, properties of the selected file or device, have some common options for read-only files, and some common options: o_trunc - if it is open already existing files, this Option Replace the newly written content O_Append - newly written content, will follow the original contents of the original content O_CREAT - If the file to be opened does not exist, then create this file, pay attention to make sure that the path created by the file Presented, and allowing new files O_CREAT | O_EXCL - forced new files in this folder, if the file already exists, the Open function returns a failure. Here is a non-common call: The create function is actually equivalent to the OPEN function with O_CREAT | O_WRONLY | O_TRUNC. About Mode Settings Using O_CREAT, you need to make some permission settings for the third optional parameter If there are some Linux / UNIX knowledge of file permissions, this setting is better understood
S_IRUSR: Read permission, ownerS_IWUSR: Write permission, ownerS_IXUSR: Execute permission, ownerS_IRGRP: Read permission, groupS_IWGRP: Write permission, groupS_IXGRP: Execute permission, groupS_IROTH: Read permission, othersS_IWOTH: Write permission, othersS_IXOTH: Execute permission, others
EG: Open ("Hello.txt", O_CREAT, S_IRUSR | S_IXOTH); This setting sets the Hello.txt's permissions to the file owner has permission, the other permissions performed - this file is only There is this two permissions to mention the flow file operation and non-flow file operation, and the conversion non-flow file operations between them, that is, the commonly used f start, such as Fopen, FREAD, etc. Let's repeat the conversion of both:
The file descriptor and file pointer transition uses two functions to easily implement both conversion relationships, which in some occasions (sometimes easy to use flow function, but sometimes you need to use the file descriptor function to do some underlying work) Still very useful. Int fileno (file * stream);
The Function Fileno Examines The Argument Stream and Returns ITS INTEGER DESCRIPTOR.
File * fdopen; file *freopen (const char * path, const char * mode, file * stream); still some attention to the ~ fdopen function, put a file pointer with Fildes' file descriptions, the value of the MODE parameter ("R", "R ", "W", "W ", "A", "A ")), which is consistent with the read and write permissions of the file represented by Fildes Setting "W" "W " in FDOpen does not cut off existing data as FOPEN, or will continue to write.
This function is often used in an interpretator obtained by creating a pipe and a network communication channel function. Because these special types of files cannot be opened with standard I / O Fopen functions, the device-specific function must first be called to obtain a file descriptor, and then use FDOPEN to combine a standard I / O stream with the descriptor.
Freopen opens a specified file (indicated by F P) (indicated by F P) (indicated by the path name), if the stream has been opened, then turn off the stream first. This function is typically used to open a specified file into a predefined stream: standard input, standard output, or standard error.
Next, in the flow I / O read / write control, there are three types of control methods in the buffer: full buffer, row buffer, block buffer: That is to say, to save the data to be sent until Fill a block, send it together; the usual file read and write is full buffer mode row buffer: that is, the newline break is read and written; commonly used for standard input stdin, standard output stdout has no buffer : There is no buffer mechanism, receive one, read one; common like standard error stream stderr can use the following function to change the file stream buffering mode int setvbuf (file * stream, char * buf, int mode, size_t size); Mode Can be: _ionbf unBuffered unbuffered _iolbf line buffered row buffer _iofbf full buffered full buffering setvbuf must use the forced refresh stream using the FFLUSH function before the OPEN file does not perform other files, can be the data in the buffer to write immediately Document INT FFLUSH (file * stream);
Document positioning: underlying I / O Use:
OFF_T
Lseek (int Fildes, OFF_T Offset, INT imce);
This system calls, sets the read-write pointers of the file descriptor, combined with Offset and iminents to determine this location.
WHENCE defines the usage of OFFSET offset value
Seek_set offset is an absolute position
Seek_cur offset is a relative position called from the current pointer position
Seek_end offset is a location from the file.
The LSEEK return value is the offset value set by the file program.
OFF_T Type Definition in Sys / Types.h
-----------------------
Positioning standard I / O stream:
Ftell fizek
For binary files, the location indication is calculated from the beginning, in bytes;
Quote
For text files, their file current location may not be measured in a simple byte displacement. Once again, this is mainly
Also in a non-U N i x system, they may store text files in different formats. In order to locate a text file,
Whence must be SEEK_SET, and OFFSET can only have two values: 0 (representing the reverberation to its starting position), or the value returned to the FTELL of the file. Using the REWIND function can also set a stream to the starting position of the file.
When the program may need to be ported to a non UNIX system,
Fgetpos and fsetpos
WHENCE defines the usage of OFFSET offset value
Seek_set offset is an absolute position
Seek_cur offset is a relative position called from the current pointer position
Seek_end offset is a location from the file.
The LSEEK return value is the offset value set by the file program.
I don't think that it will be the way the MS text file is saved and Unix.
No matter how it is saved into binary files, it is not going to process the content inside.
When MS is saved, the RN is converted into N read out, and the N is converted to RN if it is opened by text file (in the previous consideration, save user disk space)
FSEEK is under Windows or see MSDN Description Description It is to address the stream in memory or a file on disk or write a small program.
FSTAT STAT and LSTAT system call #include
Struct stat statbuf; mode_t model; stat ("filename", & statbuf); model;
FCNTL can be used to change many properties of the file, such as copying, acquiring, and setting file descriptors, changing file status, file lock.
#include
INT FCNTL (int Fildes, INT CMD);
INT FCNTL (int Fildes, Int cmd, long arg);
Fildes is the file descriptor to operate
CMD decides what operations doing Fildes
APUE:
FCNTL functions have five functions:
• Copy an existing descriptor (CMD = f_dupfd). - Function is similar to DUP / DUP2 but still some detail;
• Get / set file descriptor marks (cmd = f_getfd or f_setfd).
• Get / set the file status flag (CMD = f_getfl or f_setfl).
• Get / set asynchronous I / O enable (cmd = f_getown, or f_setown).
• Get / set record lock (cmd = f_getlk, f_setlk or f_setlkw).