现在的位置: 首页 > 综合 > 正文

《Linux程序设计第四版》第三章文件操作 读书笔记(五)

2019年07月18日 ⁄ 综合 ⁄ 共 1249字 ⁄ 字号 评论关闭

/**格式化输入输出  */
(1)输出
  #include<stdio.h>
  int printf(const char *format,...);   输出到标准输出
  int sprintf(char *s, const char *format,...);  把自己的输出和一个结尾空字符写到字符串s

  int fprintf(FILE *stream, const char *format,...);  输出到指定的文件流中

后期有时间可以研究一下printf的源码实现


(2)输入
  #include<stdio.h>
  int scanf(const char *format,...);  
  int sscanf(char *s, const char *format,...);
  int fscanf(FILE *stream, const char *format,...);  

  /** 与目录相关的系统调用与库函数 */
(1)系统调用
    #include<sys/types.h>
    #include<sys/stat.h>
    int mkdir(const char *path, mode_t mode);
    int rmdir(const char *path);
    int chdir(const char *path);
    char *getcwd(char *buf,size_t size);将当前目录的名字写到给定的缓冲区buf里,
                                        如果目录名超过size大小,则返回NULL,成功返回指针buf
    
(2)库函数
    #include<sys/types.h>
    #include<dirent.h>

    DIR *opendir(const char *name);
    struct dirent *readdir(DIR *dirp);
    long int telldir(DIR *dirp);   返回值记录着一个目录流里的当前位置
    void seekdir(DIR *dirp, long int loc);   设置目录流dirp的目录项指针,loc的值通过telldir调用获得
    int closedir(DIR *dirp);成功返回0,错误返回-1

    struct dirent   
    {   
      long d_ino; /* inode number 索引节点号 */  
         
        off_t d_off; /* offset to this dirent 在目录文件中的偏移 */  
         
        unsigned short d_reclen; /* length of this d_name 文件名长 */  
         
        unsigned char d_type; /* the type of d_name 文件类型 */  
         
        char d_name [NAME_MAX+1]; /* file name (null-terminated) 文件名,最长255字符 */  
    }

抱歉!评论已关闭.