您的位置:首页 > 运维架构 > Linux

【Linux-10】基础IO

2019-06-08 22:08 190 查看
[code]时间呐,等等我~
长大后才发现,原来时间真的有加速度的!

目录:

  • 标准库IO接口
  • 文件描述符
  • 重定向 / minishell的重定向
  • 文件流指针
  • 动态库和静态库

>>系统调用接口IO接口基本使用:

FILE*   fopen  ("r r+ w w+ a a+")   fwrite/fread   fseek   fclose 

[code]       r     Open text file for reading.  
            The stream is positioned at the beginning of the file.
             
      r+     Open for reading and writing.
            The stream is positioned at the beginning of the file.
     
      w     Truncate(缩短) file to zero length or create text file for writing.
            The stream is positioned at the beginning of the file.
     
      w+     Open for reading and writing.
            The file is created if it does not exist, otherwise it is truncated.
The stream is positioned at the beginning of the file.

      a     Open for appending (writing at end of file).  
            The file is created if it does not exist.
            The stream is positioned at the end of the file.

      a+     Open for reading and appending (writing at end of file).
            The file is created if it does not exist. The initial file position
            for reading is at the beginning of the file,
            but output is always appended to the end of the file.

标准库IO接口:FILE* fopen("r r+ w w+ a a+") fwrite/fread fseek fclose FILE* 
        功能:打开文件
        pathname:打开指定的文件名称
        flags:     选项参数
         必选参数:O_RDONLY    O_WRONLY    0_RDWR    【只能选择其一】
         可选参数:
                O_CREAT        若文件存在则打开;否则创建新文件
                O_EXCL        与O_CREAT同时使用;若文件存在则报错;不存在则创建
                O_TRUNC        打开文件同时截断文件为0长度
                O_APPEND    写数据的时候总是写到
          返回值:文件描述符(真正数) 失败:-1

>>文件描述符

文件描述符:内核中文件信息表中的一个下标--->对应有一个文件描述信息来操作文件
           文件描述符分配规则:最小未使用

一个进程运行起来之后,默认会打开三个文件:
        标准输入文件,标准输出文件,标准错误文件
        0        1        2    
        stdin    stdout    stderr


        int--->文件描述符
        FILE*--->文件流指针

文件流指针和文件描述符的关系:
    库函数和系统函数调用接口的关系是上下封装调用的关系:库函数调用的就是系统调用接口

一个数字怎么能操作一个文件呢?

 

 

>> 重定向

重定向:将要写入文件1的数据写入另一个文件;主要针对的就是文件描述符;重定向就是通过改变描述符这个下标的文件描述信息,进而改变要操作的文件。

int dup2 (int oldfd, int newfd)------>将newfd重定向到oldfd    

>> 追加重定向     O-APPEND
           >  清空重定向    O_TRUNC

minishell重定向实现:
        ls -l >> test.txt

>>动态库与静态库的生成和使用

1>库函数就是封装了一堆已编译好的代码文件,通过链接库进而获取响应函数实现;库函数中不能出现main函数。

动态库生成:--share    shared:表示生成共享库格式

静态库生成: ar -rc libmymath.a add.o sub.o

gcc选项:    -fPIC:--->编译选项,产生位置无关码
            --share:要生成的是动态库而不是可执行程序(不要main函数)
            动态库命令:lib是前缀    .so是后缀    中间是库名
        ar:静态库生成的链接
            -c:创建静态库
            -r:模块替换
            静态库命令:lib是前缀    .a是后缀    中间是库名
        库名规则:libXXX.so
    静态库生成: ar -rc libmymath.a add.o sub.o

2>库的使用: 默认链接动态库。(静态库使用的较多)
        1.生成可执行程序时--库文件的搜索路径    /lib64
            1.生成一个可执行程序时,若需要连接一个第三方库,需将这个第三方库放到指定路径下 /lib64
            2.也可以使用export LD_LIBRARY_PATH=.环境变量的设置,来设置库的链接搜索路径
            -L:设置链接时所在的路径
            3.gcc的-L选项:指定链接库的搜索路径    gcc main.o -o main –L. -lhello
        2.生成运行的程序时--动态库的加载搜索路径    /lib64    /user/lib64
            1.程序运行时,若程序是动态链接,运行时需要到指定的目录下加载到动态库    /lib64 /user/lib64
            2.也可以使用export LD_LIBRARY_PATH=.环境变量的设置,来声明程序加载时库的加载路径

 

 

 

 

 

 

~bye~

 

 

 

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: