zoukankan      html  css  js  c++  java
  • 文件和目录操作

    本章介绍的函数是库函数,而不是系统调用。
    库函数和系统调用的区别在于系统调用能够让你直接访问linux内核提供的服务,比如上一章的函数就是基于系统调用的函数。
    系统调用函数存在与内核空间,库函数都是用户模式,所以系统调用不当可能会破坏系统,但库函数调用风险就要小很多。
    库函数对I/O操作进行缓冲,减少了系统调用开销,同时可移植性也更好。
     
     
    打开和关闭文件。

    FILE *p fopen(const char *path, const char *mode);

    int fclose(FILE *stream);

    fopen以mode模式打开名为path的文件。
    fopen返回一个文件指针。
    出现错误,fopen返回NULL,并把errno设置为恰当的值。
     
     
    mode说明:
     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  trun-
                  cated.  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.
    读写文件

    size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

    size_t fwrite(void *ptr, size_t size, size_t nmemb, FILE *stream);

    参数ptr指向缓冲区保存或读取的数据。
    参数size控制记录大小。
    参数nmemb为记录数。
    函数返回读取或回写的记录数。
     
     
    fread函数例子。
    int main()
    {
        char s[] = "abc.txt";
        FILE *p = fopen(s, "r+");
        if (p == NULL)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        char buf[100];
        memset(buf, 0, sizeof(buf));
        fread(buf,sizeof(buf),1, p);
        printf("%s
    ", buf);
        fclose(p);
        }
        return 0;
    }
    fwrite函数例子。
    int main()
    {
        char s[] = "abc.txt";
        FILE *p = fopen(s, "r+");
        if (p == NULL)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        char buf[100];
        memset(buf, 0, sizeof(buf));
        sprintf(buf, "hello world
    ", "%s");
        fwrite(buf,strlen(buf),1, p);
        fclose(p);
        }
        return 0;
    }
    格式化输入和输出调用。

    int fprintf(FILE *stream, const char *fromat,…);

    int fscanf(FILE *stream, const char *fromat,…);

    熟悉C语言的人都应该熟悉printf与scanf函数,同样道理这些函数都可以用于文件输入输出操作。
     
     
    fprintf函数例子。
    int main()
    {
        char s[] = "abc.txt";
        FILE *p = fopen(s, "r+");
        if (p == NULL)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        char buf[100];
        memset(buf, 0, sizeof(buf));
        sprintf(buf, "hello world");
        fprintf(p, "%s
    ", buf);
        fclose(p);
        }
        return 0;
    }
    fscanf函数例子。
    int main()
    {
        char s[] = "abc.txt";
        FILE *p = fopen(s, "r+");
        if (p == NULL)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        char buf[100];
        memset(buf, 0, sizeof(buf));
        while (fscanf(p, "%s", buf) != EOF)
        {
            printf("%s
    ", buf);
        }
        fclose(p);
        }
        return 0;
    }
    行输入和输出调用。

    char fgets(char *s, int size, FILE *stream);

    int fputs(const char *s, FILE *stream);

    fgets从文件中读取一行,返回EOF代表文件结尾。
    fputs向文件中写入一行。
     
     
    fgets函数例子。
    int main()
    {
        char s[] = "abc.txt";
        FILE *p = fopen(s, "r+");
        if (p == NULL)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        char buf[100];
        memset(buf, 0, sizeof(buf));
        while (fgets(buf, sizeof(buf),p) != NULL)
        {
            printf("%s", buf);
        }
        fclose(p);
        }
        return 0;
    }
    fputs函数例子。
    int main()
    {
        char s[] = "abc.txt";
        FILE *p = fopen(s, "a+");
        if (p == NULL)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        char buf[100];
        memset(buf, 0, sizeof(buf));
        sprintf(buf, “hello world
    ");
        fputs(buf, p);
        fclose(p);
        }
        return 0;
    }
    文件删除和改名。

    int remove(const char *pathname);

    int rename(const char *oldpath, const char *newpath);

    remove函数删除pathname指向的文件名。
    Rename函数修改文件名称。
    执行成功返回0,失败返回-1,错误码保存在变量errno中。
     
     
    程序在运行过程中有时需要记录log,writelog函数例子。
    void writelog(const char *log)
    {
        time_t tDate;
        struct tm *eventTime;
        time(&tDate);
        eventTime = localtime(&tDate);
        int iYear = eventTime->tm_year + 1900;
        int iMon = eventTime->tm_mon + 1;
        int iDay = eventTime->tm_mday;
        int iHour = eventTime->tm_hour;
        int iMin = eventTime->tm_min;
        int iSec = eventTime->tm_sec;
        char sDate[16];
        sprintf(sDate, "%04d-%02d-%02d", iYear, iMon, iDay);
        char sTime[16];
        sprintf(sTime, "%02d:%02d:%02d", iHour, iMon, iSec);
        char s[1024];
        sprintf(s, "%s %s %s
    ", sDate, sTime, log);
        FILE *fd = fopen("my.log", "a+");
        fputs(s, fd);
        fclose(fd);
    }
    remove函数例子。
    int main()
    {
        char s[] = "abc.txt";
        int i = remove(s);
        if (i == -1)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        }
        return 0;
    }
    rename函数例子。
    int main()
    {
        char s[] = "abc.txt";
        int i = rename(s,"aaa.txt");
        if (i == -1)
        {
            printf("error is %s
    ", strerror(errno));
        }
        else
        {
            printf("success
    ");
        }
        return 0;
    }
    找到当前目录。

    char *getcwd(char *buf,size_t size);

    getcwd函数把当前工作目录的绝对路径名复制到buf中,size指示buf的大小。
    如果buf不够大,不能装下整个路径名,getcwd返回NULL。
     
     
     
    获得目录列表。
      –用opendir函数打开目录文件。
      –用readdir函数读出目录文件内容。
      –用closedir函数关闭目录文件。
      –这些函数都在<dirent.h>中声明。

    DIR *opendir(const char *pathname);

    struct dirent *readdir(DIR *dir);

    int closedir(DIR *dir);

    Opendir函数打开pathname指向的目录文件,如果错误返NULL
     
     
    获取目录列表例子。
    int main(int argc, char *argv[])
    {
        if (argc < 2)
            return 0;
        DIR *dp;
        struct dirent *dirp;
        dp = opendir(argv[1]);
        if (dp == NULL)
        {
            printf("error is %s
    ", strerror(errno));
            return 0;
        }
        while((dirp = readdir(dp)) != NULL)
        {
            printf("%s
    ", dirp->d_name);
        }
        closedir(dp);
        return 0;
    }
  • 相关阅读:
    超哥笔记--linux准备知识(1)
    爬虫系列---scrapy全栈数据爬取框架(Crawlspider)
    爬虫系列---scrapy post请求、框架组件和下载中间件+boss直聘爬取
    pymongo 一篇文章搞定
    一篇文章搞定mongodb
    python进阶(四) windows下虚拟环境使用
    java基础(四) -变量类型
    java基础(二) -对象和类
    java基础(一) -语法
    Linux常用命令大全
  • 原文地址:https://www.cnblogs.com/shichuan/p/4496328.html
Copyright © 2011-2022 走看看