Linux systems commonly used commands (5)

1. read, write function to read and write files

Example: The contents of a file is written to another file.

Results of the:

Description has been successfully written to the contents of a file to another file.

2. lseek function

off_t fact is an integer, a file descriptor FD, offset is the offset of the reading and writing position, the position is The whence it is divided into three: SEEK_SET, SEEK_CUR, SEEK_END.

(1) Get File Size

Expand (2) file

Example: From the end of the file to expand the 2000 bytes backwards.

Results of the:

Finally we found the file size is 2015 bytes, because the final will be written in a byte to the inside.

After expanding the file called empty file.

3. stat function

For more information see the documents: (1) stat command

(2) stat function:

Where path is the incoming parameters, the path to the file is passed. buf argument is heard. buf is a pointer to the structure, function stat will write data has been prepared in the structure pointed to by buf, the buf parameter is outgoing. stat function return value: If 0 is described successful, or -1 if the error described.

St_mode which is 16 variables:

The st_mode with "and" get on the corresponding mask corresponding results. For example st_mode & S_IRWXO, get is that three others on behalf of the authority.

4. stat and lstat

(1) Example: stat function application: The size is the output file.

Results of the:

Note: The command line arguments argv [0] is the name of the current executable file, so we want to argv [1] incoming stat function.

If the argument passed is a soft link to the file, it will still function stat to track the file, the output is the size of the file, rather than the size of the soft link file:

(2) lstat function

If the function program into lstat stat function, then the two will output the above experiments the following results:

You can see if the incoming parameter is a soft link to the file, it will not lstat function to track the file, the output is the size of a soft link to the file.

In addition, these commands can also be divided into tracking and not tracking:

5. chmod function

6. chown function

7. truncate function

Expand the file: truncate function

8. link function, symlink function, readlink function

9. unlink创建临时文件

(1)unlink函数作用:删除一个文件的目录项并减少它的链接数,若成功返回0,否则返回-1。如果想通过调用这个函数来成功删除文件,就必须拥有这个文件的所属目录的写和执行权限。

(2)unlink函数使用:

         1)如果是符号链接,则删除符号链接

         2)如果是硬链接,硬链接数减1,当减为0时,释放数据块和inode。

         3)如果文件硬链接数为0,但有进程已打开该文件,并持有文件描述符,则等该进程关闭该文件时,kernel才真正去删除该文件。(利用该特性创建临时文件,先open,然后调用unlink并不会真正删除,接下来还可以对该文件进行读写操作,最后当close文件后,该文件才会真正被删除。)

例:

注:

(1)在执行write(fd, "hello\n", 6);后,文件指针已经不在文件的起始位置了,所以我们要重置文件指针,然后才能从文件中读数据。重置文件指针的函数为:

(2)将读出的内容写到屏幕上,文件描述符为1(标准输出)。

10. 目录操作函数

(1)chdir函数、getcwd函数

例:

在进程中修改当前进程的路径到上一级目录,并在上一级目录创建一个文件chdir.txt,然后将当前进程的工作目录打印出来。注:shell的工作目录并没有改变。

执行结果:

(2)mkdir函数

(3)rmdir函数

(4)opendir函数

(5)readdir函数

例:递归读目录获取文件个数

getFileNum函数传入的是目录所在路径,返回的是该目录中的普通文件个数。readdir函数如果返回不为NULL,说明该目录中有目录或者文件,那么进入while循环,while循环会自动地遍历该目录中的文件或目录;否则如果readdir函数返回NULL,说明该目录为空目录,那么就不进入while循环了。while循环退出后,total保存的就是该目录中的普通文件个数。关闭目录,然后返回total。

      

执行结果:

11. dup和dup2

(1)dup函数返回的是文件描述符表中没有被占用的最小的文件描述符。

比如dup(3),该函数会返回4,因为4是文件描述符表中没有被占用的最小的文件描述符。然后3和4就都是指向该文件的文件描述符了。

(2)dup2(old, new)   如果old和new不同,那么函数返回后,old和new就都是指向old指向文件的文件描述符,并且如果new之前指向打开的文件的话,会先关闭该文件。如果old和new相同的话,该函数无实际操作,直接返回old。

注:当关闭文件时,要用close函数将指向该文件的两个文件描述符都关掉。

12. fcntl函数

函数原型:

当cmd参数是F_GETFL时,我们将arg设为0,那么函数的返回值是上图中F_GETFL右侧的7种之一,表示当前文件的状态。

当cmd参数是F_SETFL时,我们将arg设置为O_开头的宏,那么该文件会设置为相应的状态。

函数的返回值如果为-1代表失败。

例:

执行结果:

可以发现字符串p是从文件的头部开始写,并覆盖原来的内容。将文件状态修改之后,再次输入新内容q,可以发现该内容会追加到旧内容的后面。

 

Guess you like

Origin blog.csdn.net/mengyujia1234/article/details/90700631