Http://blog.csdn.net/Grong_H/archive/2009/12/02/4923332.aspx
Linux I/O function set
---------- Header file description ------------------
Sys/types. h: Common variable type
Sys/STAT. h: File status
Fcntl. h: file control
Unstd. h: lseek, read, write...
------------- File I/O -----------------
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
Int open (const char * pathname, int Oflag..., [mode_t mode]);
3rd parameters are written as.... This is the method that ansi c describes the number and type of the remaining parameters that can be changed.
O_rdonly: Read-only,
O_wronly: Write-only open,
O_rdwr: read and write operations.
O_append: it is added to the end of the file every time it is written.
O_creat: Creates a file if it does not exist. When you use this option, you must specify the third parameter mode.
O_excl: If o_creat is specified at the same time and the file already exists, an error occurs. This allows you to test whether a file exists. If not, create the file as an atomic operation.
O_trunc: if the file exists and is read-only or only successfully opened, the length of the file is truncated to 0.
O_noctty: If pathname refers to a terminal device, the device is not allocated as the control terminal of the process.
O_nonblock: If the pathname refers to a FIFO, block-specific, or character-specific file, this item sets the non-blocking mode for opening the file and subsequent I/O operations.
O_ndelay (no delay) indicates that earlier System V is introduced. It is similar to the o_nonblock (no blocking) option,
O_sync: enables each write operation to wait until the physical I/O operation is completed.
Int create (const char * pathname, mode_t mode );
# Include <unistd. h>
Int close (INT filedes );
# Include <sys/types. h>
# Include <unistd. h>
Off_t lseek (INT filedes, off_t offset, int whence );
Whence value: seek_set seek_cur seek_end
# Include <unistd. h>
Size_t read (INT filedes, void * buff, size_t nbytes );
Size_t write (INT filedes, const void * buff, size_t nbytes );
# Include <unistd. h>
Int DUP (INT filedes );
Int dup2 (INT filedes, int filedes2 );
# Include <sys/types. h>
# Include <unistd. h>
# Include <fcntl. h>
Int fcntl (INT filedes, int cmd,... [int Arg]);
# Include <unistd. h>
# Include <sys/IOCTL. h>
Int IOCTL (INT filedes, int request ,...);
------------- Standard I/O Library ---------------
Standard I/O library functions require # include <stdio. h>
Void setbuf (File * FP, char * BUF );
Int setvbuf (File * FP, char * Buf, int mode, size_t size );
Int fflush (File * FP );
File * fopen (const char * pathname, const char * type );
File * freopen (const char * pathname, const char * type, file * FP );
File * fdopen (INT filedes, const char * type );
Int ferror (File * FP );
Int feof (File * FP );
Int GETC (File * FP );
Int fgetc (File * FP );
Int getchar (void );
Int ungetc (int c, file * FP );
Int putc (int c, file * FP );
Int fputc (int c, file * FP );
Int putchar (int c );
Char * fgets (char * Buf, int N, file * FP );
Char * gets (char * BUF );
Int fputs (const char * STR, file * FP );
Int puts (const char * Str );
Size_t fread (void * PTR, size_t size, size_t nobj, file * FP );
Size_t fwrite (const void * PTR, size_t size, size_t nobj, file * FP );
Logn ftell (File * FP );
Int fseek (File * FP, long offset, int whence );
Void rewind (File * FP );
Int fgetpos (File * FP, fpos_t * POS );
Int fsetpos (File * FP, const fpost_t * POS );
Int printf (cosnt char * format ,...);
Int fprintf (File * FP, const char * format ,...);
Int sprintf (char * Buf, const char * format ,...);
Int scanf (const char * format ,...);
Int fscanf (File * FP, const char * format ,...);
Int sscanf (const char * Buf, const char * format ,...);
# Include <stdarg. h>
Int vprintf (const char * format, va_list Arg );
Int vfprintf (File * FP, const char * foramt, va_list Arg );
Int vspritnf (char * Buf, const char * format, va_list Arg );
Int fileno (File * FP );
Char * tmpname (char * PTR );
File * tmpfile (void );
Char * tmpname (const char * directory, const char * prefix );
------------- File and directory --------------
Get file attributes
# Include <sys/types. h>
# Include <sys/STAT. h>
Int Stat (const char * file_name, struct stat * BUF );
Int fstat (INT filedes, struct stat * BUF );
Struct stat {
Dev_t st_dev; // Device
Ino_t st_ino; // Node
Mode_t st_mode; // Mode
Nlink_t st_nlink; // hard connection
Uid_t st_uid; // user ID
Gid_t st_gid; // group ID
Dev_t st_rdev; // device type
Off_t st_off; // number of file bytes
Unsigned long st_blksize; // block size
Unsigned long st_blocks; // number of blocks
Time_t st_atime; // last access time
Time_t st_mtime; // last modification time
Time_t st_ctime; // the last change time (attribute)
};
S_islnk (st_mode): whether it is a connection.
S_isreg is a regular file.
S_isdir is a directory
S_ischr is a character device
S_isblk is a block Device
S_isfifo is a FIFO file.
S_issock is a socket file.
Determine whether you have the permission to access files
# Include <unistd. h>
Int access (const char * pathname, int mode); // r_ OK, w_ OK, x_ OK, f_ OK
Change File Permissions
# Include <sys/types. h>
# Include <sys/STAT. h>
Mode_t umask (mode_t cmask );
Int chmod (const char * pathname, mode_t mode );
Int fchmod (INT filedes, mode_t mode );
The mode parameter has the following combinations:
S_isuid 04000 file (Set User-ID on execution) Bit
S_isgid 02000 (set group-ID on execution) bit of the file
S_isvtx 01000 file Sticky Bit
S_irusr (s_iread) 00400 the file owner has the read permission
S_iwusr (s_iwrite) 00200 the file owner has the write permission
S_ixusr (s_iexec) 00100 the file owner has executable permissions
S_irgrp 00040 user group with read permission
S_iwgrp 00020 user group with write permission
S_ixgrp 00010 user group with executable permissions
S_iroth 00004 other users have read permission
S_iwoth 00002 other users have write permission
S_ixoth 00001 other users have executable permissions
Only when the owner of the file or valid user identification code is 0 Can the permission of the file be modified.
Change the file owner
# Include <sys/types. h>
# Include <unistd. h>
Int chown (const char * pathname, uid_t owner, gid_t group );
Int fchown (INT filedes, uid_t owner, gid_t group );
File Truncation
# Include <sys/types. h>
# Include <unistd. h>
Int truncate (const char * pathname, off_t length );
Int ftruncate (INT filedes, off_t length );
Hard connection
# Include <unistd. h>
Int Link (const char * existingpath, const char * newpath );
Int unlink (const char * pathname );
Symbolic connection
# Include <unistd. h>
Int symlink (const char * actualpath, const char * sympath );
Int readlink (const char * pathname, char * Buf, int bufsize );
Delete a file or directory
# Include <stdio. h>
Int remove (const char * pathname );
Rename a file or directory
# Include <stdio. h>
Int Rename (const char * oldname, const char * newname );
Access and modification time
# Include <sys/types. h>
# Include <utime. h>
Int utime (const char * pathname, const struct utimbuf * times );
Struct utimbuf {
Time_t actime; // access time
Time_t modtime; // modefication time
}
Create and delete directory
# Include <sys/types. h>
# Include <sys/STAT. h>
Int mkdir (const char * pathname, mode_t mode );
# Include <unistd. h>
Int rmdir (const char * pathname );
Read directory
# Include <sys/types. h>
# Include <dirent. h>
Dir * opendir (const char * pathname );
Struct dirent * readdir (dir * DP );
Void rewinddir (dir * DP );
Int closedir (dir * DP );
Struct dirent
{
Ino_t d_ino;
Ff_t d_off;
Signed short int d_reclen;
Unsigned char d_type;
Har d_name [256];
};
Current Directory and changes
# Include <unistd. h>
Int chdir (const char * pathname );
Int fchdir (INT filedes); // descriptor of the file directory.
Char * getcwd (char * Buf, size_t size );
Kernel File Cache refresh
# Include <unistd. h>
Void sync (void); // only caches all modified blocks into the write queue and does not wait for actual I/O operations.
Int fsync (INT filedes); // wait until the file is written to the device
------------- Advanced I/O -----------------
Set non-blocking I/O
Set o_nonblock or fcntl to set o_nonblock during open.
Record lock
# Include <sys/types. h>
# Include <unistd. h>
# Include <fcntl. h>
Int fcntl (INT filedes, int cmd, struct flock * flockptr );
CMD value: f_getlk f_setlk f_setlkw
Struct flok {
Short l_type; // f_rdlck f_wrlck f_unlck
Off_t l_start; // offset in bytes, relative to l_whence
Short l_whence; // seek_set, seek_cur, seek_end
Off_t l_len; // length, in bytes, 0 means lock to EOF
Pid_t l_pid; // return with f_getlk
};
I/O multi-channel Transfer
# Include <sys/types. h>
# Include <sys/time. h>
# Include <unistd. h>
Int select (INT maxfdpl, fd_set * readfds, fd_set * writefds, fd_set * limit TFDs, struct timeval * tvptr );
Struct timeval {
Long TV _sec;
Long TV _usec;
};
Distributed read and clustered write
# Include <sys/types. h>
# Include <sys/uio. h>
Size_t readv (INT filedes, const struct iovec [], int iovcnt );
Size_t writev (INT filedes, const struct iovec [], int iovcnt );
Struct iovec {
Void * iov_base;
Size_t iov_len;
};
Storage ing I/O
# Include <sys/types. h>
# Include <sys/Mman. h>
Caddr_t MMAP (caddr_t ADDR, size_t Len, int Prot, int flag, int filedes, off_t off );
Prot value: prot_read prot_write prot_exec prot_none
Flag value: map_fixed map_shared map_private
Int munmap (caddr_t ADDR, size_t Len );
Windows C language I/O function set
------------- File I/O -----------------
Int open (char * pathname, int access,... [, int permiss])
The access value is shown in the table below.
┌ ── ─ ┬ ── ─ ┐
│ Access value │ meaning │
├ ── ─ ┼ ── ─ ┤
│ O_rdonly │ read the file │
│ O_wronly │ write a file │
│ O_rdwr │ read and write │
│ O_ndelay │ not used; compatible with Unix systems │
│ O_append │ read and write, but every write is always added at the end of the file │
│ O_creat │ if the file exists, this mark is useless; if it does not exist, create a new file │
│ O_trunc │ if the file exists, the length is truncated to 0, and the attribute remains unchanged │
│ O_excl │ unused; compatible with Unix systems │
│ O_binary │ this flag can be displayed to open a file in binary format │
│ O_text │ this flag can be used to display and open a file in text format │
└ ── ─ ┴ ── ─ ┘
The third parameter is used only when a new file is created. permiss is the file attribute and can be set to the following values:
S_iwrite: s_iread | s_iwrite: Read and Write
Int creat (char * filename, int permiss)
Permiss indicates file read/write. It can be the following values:
S_iwrite: s_iread | s_iwrite: Read and Write
Int creatnew (char * filenamt, int attrib)
Attrib is a File Attribute and can be the following values:
Fa_rdonly read-only fa_hidden hide fa_system
Int creattemp (char * filenamt, int attrib)
Attrib is a File Attribute and can be the following values:
Fa_rdonly read-only fa_hidden hide fa_system
Int read (INT handle, void * Buf, int nbyte)
Int write (INT handle, void * Buf, int nbyte)
Int DUP (INT handle)
Int dup2 (INT handle, int newhandle)
Int EOF (int * handle)
Long filelength (INT handle)
Int setmode (INT handle, unsigned Mode)
This function is used to set how to open a handle file.
Int getftime (INT handle, struct ftime * ftime)
The time when the handle file is read,
Save the file time in the ftime structure, and 0 is returned successfully. The ftime structure is as follows:
┌ ── ─ ┐
│ Struct ftime │
│ {│
│ Unsigned ft_tsec: 5; // second │
│ Unsigned ft_min: 6; // minute │
│ Unsigned ft_hour: 5; // hour │
│ Unsigned ft_day: 5; // │
│ Unsigned ft_month: 4; // month │
│ Unsigned ft_year: 1; // annual-1980 │
│} │
└ ── ─ ┘
Int setftime (INT handle, struct ftime * ftime)
Time when the file number is handle is overwritten,
The new time is in the ftime structure. 0 is returned successfully. The ftime structure is as follows:
┌ ── ─ ┐
│ Struct ftime │
│ {│
│ Unsigned ft_tsec: 5; // second │
│ Unsigned ft_min: 6; // minute │
│ Unsigned ft_hour: 5; // hour │
│ Unsigned ft_day: 5; // │
│ Unsigned ft_month: 4; // month │
│ Unsigned ft_year: 1; // annual-1980 │
│} │
└ ── ─ ┘
Long lseek (INT handle, long offset, int fromwhere)
Seek_set file switch seek_cur current position seek_end file end
Long tell (INT handle)
Int isatty (INT handle)
Int lock (INT handle, long offset, long length) blocks file sharing
Int unlock (INT handle, long offset, long length)
Int close (INT handle)
------------- Standard I/O Library ---------------
# Include <stdio. h>
File * fopen (char * filename, char * type)
Type can be suffixed with the following string
── ┬ ── ─
│ Type │ read/write │ text/2 hexadecimal file │ new/open old file │
── ┼ ── ─
│ R │ read │ text │ open old file │
│ W │ write │ text │ create a new file │
│ A │ add │ text │ If yes, open it. If no, create a new │
│ R + │ read/write │ unlimited │ open │
│ W + │ read/write │ no restrictions │ create a new file │
│ A + │ read/Add │ no restrictions │ If yes, open it. If no, create a new │
── ┴ ── ─
The suffixes T and B can be added. Adding B indicates that the file is operated in binary format and T is unnecessary.
File * fdopen (INT ahndle, char * type)
File * freopen (char * filename, char * type, file * Stream)
Int GETC (File * Stream)
Int putc (int ch, file * Stream)
Int getw (File * Stream)
Int putw (int w, file * Stream)
Int ungetc (char C, file * Stream)
Int fgetc (File * Stream)
Int fputc (int ch, file * Stream)
Char * fgets (char * string, int N, file * Stream)
Int fputs (char * string, file * Stream)
Int fread (void * PTR, int size, int nitems, file * stream) (Binary I/O)
Int fwrite (void * PTR, int size, int nitems, file * stream) (Binary I/O)
Int fscanf (File * stream, char * Format [, argument,…])
Int vfscanf (File * stream, char * format, valist PARAM)
Int fprintf (File * stream, char * Format [, argument,…])
Int vfprintf (File * stream, char * format, valist PARAM)
Int fseek (File * stream, long offset, int fromwhere)
Fromwhere can be the following values:
Seek_set file switch seek_cur current position seek_end file end
Long ftell (File * Stream)
The function returns the pointer position of the current file located in stream, expressed in bytes.
Int rewind (File * Stream)
Move the current file pointer stream to the beginning of the file
Int feof (File * Stream)
Int fileno (File * Stream)
Process the files on the stream and return the files for processing.
Int ferror (File * Stream)
Void clearerr (File * Stream)
Void setbuf (File * stream, char * BUF)
Specify a buffer Buf for stream
Void setvbuf (File * stream, char * Buf, int type, unsigned size)
Specify a buffer Buf for stream. The buffer size is size and the type is type. The value of type is shown in the table below.
The client device is in the row cache; otherwise, the device is in the full cache.
┌ ── ─ ┐
│ Type value │ meaning │
├ ── ─ ┤
│ _ Iofbf │ the file is a full buffer. When the buffer is empty, the next input operation will attempt to fill up the entire cache │
│ Punch zone. During output, the buffer zone will be fully filled before writing any data to the file. │
│ _ Iolbf │ the file is a row buffer. When the buffer is empty, the next input operation will still attempt to fill in the entire │
│ Punch zone. However, during output, the buffer zone is cleaned whenever a new line character is written to a file. │
│ _ Ionbf │ the file is unbuffered. The Buf and size parameters are ignored. Each input operation will be directed to │
│ Read. Data is immediately written to the file for each output operation. │
└ ── ─ ┘
Int fclose (File * stream) disables a stream, which can be a file or device (such as LPT1)
Int fcloseall () disables all streams except stdin or stdout.
Int fflush (File * Stream)
Int fflushall ()
------------- File and directory --------------
Int access (char * filename, int amode) This function checks the file filename and returns the file attributes,
The function stores attributes in amode, which is composed of the following digits.
06 read and write 04 read 02 write 01 execute (ignored) 00 file exist
If filename is a directory, the function only determines whether the directory exists.
If the function is successfully executed, 0 is returned. Otherwise,-1 is returned.
Int chmod (char * filename, int permiss) This function is used to set the filename attribute of a file.
Permiss can be the following values:
S_iwrite: s_iread | s_iwrite: Read and Write
Char * _ fullpath (char * abspath, const char * relpath, size_t maxlength );
Char * _ mktemp (char * template );
Int remove (const char * path );
Int Rename (const char * oldname, const char * newname );
Void _ makepath (char * path, const char * drive, const char * Dir, const char * fname, const char * ext );
Void _ splitpath (const char * path, char * drive, char * Dir, char * fname, char * ext );
Int _ Stat (const char * path, struct _ stat * buffer );
Int _ umask (INT pmode );
Int _ unlink (const char * filename );
Directory function. The function library is dir. h and DOS. h.
Int chdir (char * path) converts the specified directory path (for example, "C: \ WPS") to the current working directory. 0 is returned successfully.
Int findfirst (char * pathname, struct ffblk * ffblk, int attrib) to find the specified file. 0 is returned.
Pathname is the specified directory name and file name, for example, "C: \ WPS \ TXT"
Ffblk is a specified structure for saving file information. It is defined as follows:
When there are too many threads, there are too many threads, too many threads.
Explain struct ffblk success
Detail {detail
Optional char ff_reserved [21]; // reserved DOS characters
Optional char ff_attrib; // specifies the file attribute.
Optional int ff_ftime; // file time limit
Optional int ff_fdate; // specifies the file date.
Optional long ff_fsize; // The object length.
Invalid char ff_name [13]; // the file name is invalid.
} Else
When there are too many threads, there are too many threads, too many threads.
Attrib is a file attribute, represented by the following characters
When there are too many threads, there are too many threads, too many threads.
┃ Fa_rdonly read-only file named internal fa_label volume number Internal
Invalid fa_hidden: Hide the file named "invalid fa_direc ".
Invalid fa_system System File internal fa_arch file external
When there are too many threads, there are too many threads, too many threads.
Example:
Struct ffblk ff;
Findfirst ("*. wps", & ff, fa_rdonly );
Int findnext (struct ffblk * ffblk) to obtain the finddirst-matched file. 0 is returned.
Void fumerge (char * path, char * drive, char * Dir, char * Name, char * ext)
This function uses drive (C:, A:, etc.), path Dir (\ TC, \ BC \ Lib, etc ),
File Name (TC, WPS, etc.), extension ext (. EXE,. com, etc.) to form a file name
Stored in the path.
Int fnsplit (char * path, char * drive, char * Dir, char * Name, char * ext)
This function breaks down the file name path into drive (C:, A:, etc.), path Dir (\ TC, \ BC \ Lib, etc ),
File Name (TC, WPS, etc.), extension ext (. EXE,. com, etc.), and respectively stored in the corresponding variables.
Int getcurdir (INT drive, char * direc) This function returns the name of the current working directory of the specified drive
Drive specified drive (0 = Current, 1 = A, 2 = B, 3 = C, etc)
If direc successfully saves the variable in the current working path of the specified drive, 0 is returned.
Char * getcwd (char * Buf, iint N) This function takes the current working directory and stores it in Buf until n Bytes are long. The error returns NULL.
Int getdisk () returns an INTEGER (0 = A, 1 = B, 2 = C, etc.) from the drive currently in use)
Int setdisk (INT drive) sets the drive to be used (0 = A, 1 = B, 2 = C, etc.), returns the total number of available drives
Int mkdir (char * pathname) creates a new directory pathname and returns 0.
Int rmdir (char * pathname) deletes a directory pathname, and 0 is returned.
Char * mktemp (char * template) is used to construct a file name that does not exist in the current directory and coexist in the template.
Char * searchpath (char * pathname) uses msdos to find the path of the filename file. This function uses the PATH variable of Dos. If no file is found, null is returned.