Linux I/O operations, file I/O Summary

Source: Internet
Author: User
Tags lstat strtok

File IO is not cached, and each read and write calls the corresponding system call in the kernel.
File IO common functions:
Open, close, read, write, lseek
For the kernel, all open files have file descriptor references.
The file descriptor is a non-negative integer. When you open an existing file or create a new file, you and return a file descriptor to the process.
When reading or writing a file, use the file descriptor returned by open to identify the file and pass it as a parameter to read or write.

1. Open (open file name (including file path), int flag, Mode)
Falg: o_rdonly, o_wdonly, o_rdwr, o_creat, o_excl (if an error message is returned)
O_trunc (delete the data in the file if it already exists)
2. Read (FD, Buf, size_t count)
The number of bytes read after the call is successful.
If 0 is returned, it indicates that the end of the file is reached.
If-1 is returned, it indicates an error. The error code is set through errno.
3. Write ()
4. lseek (FD, offset, whence)

5. open the file directory opendir ()

6. Get File Attribute functions: these functions are quite important.
Stat () obtains the information structure related to this naming file.
Fstat () obtains information about the file opened on the descriptor filedes.
Lstat () returns information about the symbolic link, instead of the file information referenced by the symbolic link.

St_mode is the attribute description of the opened file.

S_ifmt: 0x070000

Switch (St. st_mode & s_ifmt) // determine the type of file
{
Case s_ifreg: printf ("-"); break;
Case s_ifdir: printf ("D"); break;
Case s_iflnk: printf ("L"); break;
Case s_ifblk: printf ("B"); break;
Case s_ifchr: printf ("C"); break;
Case s_ififo: printf ("p"); break;
Case s_ifsock: printf ("S"); break;
}

S _... is some internal macro defined. Let's take a look at the specific man.

7. Check the getopt function. getopt is used to identify characters starting.

While (CH = getopt (argc, argv, "La "))! = EOF)
{
Switch (CH)
{
Case 'A ':
Printf ("option A is set \ n ");
Aflag = 1;
Break;
Case 'l ':
Printf ("option l is set \ n ");
Lflag = 1;
Break;
Default:
Printf ("wrong option % C is set \ n", optopt );
}
}

And getpwuid (St. st_uid); getgrgid (St. st_gid); functions are used to obtain the current user name and user group name.

8. the strtok () function is a string truncation operation. In the following example, space is used for truncation.

This function is weird. The first value must be Buf, and the first parameter in the future is null. This is quite disgusting ....

Arg [I ++] = strtok (BUF ,"");
Do
{
Arg [I ++] = strtok (null ,"");
} While (ARG [I-1]! = NULL );

9. Functions for folder operations: dir * opendir (); struct dirent * readdir (): Internal pointer, You can automatically move the next file in the folder until it is empty)

 

Example: use them to copy files # include <stdio. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # include <string. h> int main (INT argc, char ** argv) {int fd1, fd2; char Buf [1024]; int nbyte; If (argc! = 3) {printf ("using: % s srcfilename decfilename \ n", argv [0]); Return-1;} If (fd1 = open (argv [1], o_rdonly) <0) {perror ("open"); Return-1;} If (fd2 = open (argv [2], o_wronly | o_creat | o_trunc, 0666 )) <0) {perror ("open"); Return-1 ;}while (nbyte = read (fd1, Buf, sizeof (BUF)> 0) {write (fd2, Buf, nbyte) ;}close (fd1); close (fd2); Return 0 ;}open the file directory opendir () # include <stdio. h> # include <sys /Types. h> # include <dirent. h> int main (INT argc, char ** argv) {dir * dir; struct dirent * dirent; Dir = opendir (argv [1]); while (dirent = readdir (DIR ))! = NULL) {printf ("% s \ n", dirent-> d_name) ;}} get the file property function: Stat () obtain the information structure fstat () related to this naming file. obtain the information about the file opened on the descriptor filedes. lstat () returns the information about this symbolic link, instead of the file information referenced by the symbolic link, use stat to implement the basic functions of LS: # include <sys/types. h> # include <sys/STAT. h> # include <unistd. h> # include <stdio. h> # include <GRP. h> # include <PWD. h> # include <time. h> int main (INT argc, char ** argv) {struct stat st; int I; struct passwd * PW; struct Group * gr; struct TM * TM; stat (argv [1], & St); Switch (St. st_mode & s_ifmt) // determine the type of file {Case s_ifreg: printf ("-"); break; Case s_ifdir: printf ("D"); break; Case s_iflnk: printf ("L"); break; Case s_ifblk: printf ("B"); break; Case s_ifchr: printf ("C"); break; Case s_ifififo: printf ("p"); break; Case s_ifsock: printf ("S"); break;} for (I = 8; I> = 0; I --) {If (St. st_mode & (1 <I) // st. the last 8 digits of st_mode. The File Permission is determined by {Switch (I % 3) {Case 2: printf ("R"); break; Case 1: printf ("W"); break; case 0: printf ("X"); break;} else printf ("-");} PW = getpwuid (St. st_uid); GR = getgrgid (St. st_gid); printf ("% 2D % S % 4ld", St. st_nlink, PW-> pw_name, GR-> gr_name, St. st_size); TM = localtime (& St. st_ctime); printf ("% 04d-% 02d-% 02d % 02d: % 02d", TM-> tm_year + 1900, TM-> tm_mon + 1, TM-> tm_mday, TM-> tm_hour, TM-> tm_min); printf ("% s \ n", argv [1]); Return 0 ;}

 

 

 

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.