当前位置: 首页 > news >正文

自学嵌入式 day26 - 系统编程 文件io 目录操作

④lseek:off_t lseek(int fd, off_t offset, int whence);
功能:定位光标的位置
参数:fd:文件描述符
        offset:偏移量
        正:向后偏移
        负:向前偏移
        零:不偏移
        whence:
                SEEK_SET
                SEEK_CUR
                SEEK_END
返回值:成功返回偏移量
            失败返回-1

#include<stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>


int main()
{
    int fd = open("1.txt",O_WRONLY | O_CREAT | O_TRUNC,0666);
    if(-1 == fd)
    {
        printf("open error\n");
        return 1;
    }
    off_t offset = lseek(fd,10,SEEK_SET);
    printf("%ld\n",offset);
    write(fd,"a",2);
    close(fd);
    return 0;
}

⑤fileno:int fileno(FILE *stream);
 功能:获得一个文件流指针中的文件描述符
 参数:stream:文件流指针
返回值:成功返回文件描述符
            失败返回-1

#include <stdio.h>
#include <unistd.h>
int    main(int argc, char **argv)
{
    FILE* fp = fopen("1.txt","w");
    if(NULL == fp)
    {
        return 1;
    }

    int fd = fileno(fp);
    write(fd,"hello",5);
    //close(); fclose();
    fclose(fp);
    return 0;
}

⑥fdopen: FILE *fdopen(int fd, const char *mode);
 功能:将文件描述符转化为文件流指针
 参数:fd:已经打开的文件描述符
        mode:"r"(只读)"r+"(读写)"w"(写)"w+"(读写)"a"(添加)"a+"
 返回值:成功返回文件流指针
             失败返回NULL

#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int    main(int argc, char **argv)
{
    int fd = open("1.txt",O_RDONLY);
    if(-1 == fd)
    {

        return 1;
    }

    FILE* fp = fdopen(fd,"r");
    if(NULL == fp)
    {
         return 1;
    }
    char buf[512]={0};
    fgets(buf,sizeof(buf),fp);
    printf("%s",buf);

    fclose(fp);
    return 0;
}

⑦perror:void perror(const char *s)

功能:系统报错

参数:s:错误说明符

范围:只能用于系统库函数和标准c库函数

#include<stdio.h>
#include<errno.h>

int main()
{
    FILE *fp = fopen("10.txt","r");
    if(NULL == fp)
    {
        perror("fopen main.c : 9");
        return 0;
    }
    return 0;
}

文件操作权限(fopen 和 open):

4.目录操作

(1)操作步骤:① 打开目录 ②读取目录 ③关闭目录(可将目录当成文件看,只是操作函数和文件不一样)

(2)函数:

①打开目录 opendir:DIR *opendir(const char *name);(DIR *:代表整个目录)
功能:打开一个目录获得一个目录流指针
参数:name:目录名
返回值:成功返回目录流指针
              失败返回NULL

#include<stdio.h>
#include<dirent.h>
#include<sys/types.h>

int main()
{

    DIR *dir = opendir("../");
    if(NULL == dir)
    {
        perror("opendir\n");
        return 1;
    }
    closedir(dir);
    return 0;
}

②读取目录 readdir:struct dirent *readdir(DIR *dirp);
功能:从目录流中读取文件信息并将保存信息的结构体地址返回
参数:dirp:目录流指针
返回值:包含文件信息的结构体
            出错或者读到目录流末尾返回NULL

#include<stdio.h>
#include<dirent.h>
#include<sys/types.h>

int main()
{

    DIR *dir = opendir("../");
    if(NULL == dir)
    {
        perror("opendir\n");
        return 1;
    }
    while(1)
    {
        struct dirent *info = readdir(dir);
        if(NULL == info)
        {
            break;
        }
        printf("%s\n",info -> d_name);
    }
    closedir(dir);
    return 0;
}

③关闭目录 closedir: int closedir(DIR *dirp);
 功能:关闭之前已经打开的目录流对象
 参数:opendir的返回结果中目录流对象
 返回值:成功  0
               失败   -1;

 closedir(dir);

注:(1)用程序实现系统操作 ll 功能:

#include <stdio.h>
#include <sys/types.h>

int main(int argc, char **argv)
{
  DIR *dir = opendir("../");
  if (NULL == dir)
    {
      perror("opendir");  // stderr
      return 1;
    }

  while (1)
    {
      struct dirent *info = readdir(dir);
      if (NULL == info)
        {
          break;
        }

      printf("%s\t\t", info->d_name);
      switch (info->d_type)
        {
          case DT_BLK:
            printf("块设备\n");
            break;
          case DT_DIR:
            printf("目录文件\n");
            break;
          case DT_REG:
            printf("普通文件\n");
            break;
          case DT_CHR:
            printf("字符设备\n");
            break;
          case DT_FIFO:
            printf("管道\n");
            break;
          case DT_LNK :
            printf("符号链接\n");
            break;
          case DT_SOCK :
            printf("系統文件\n");
            break;

            default:
            printf("其他文件\n");
        }
    }
  closedir(dir);
  // system("pause");
  return 0;
}

(2)关于时间处理函数:

#include <stdio.h>
#include <time.h>


int    main(int argc, char **argv)
{
    time_t tm;
    tm = time(NULL);

    printf("%ld\n",tm);

    struct tm * tminfo = localtime(&tm);
    printf("%d-%d-%d %d:%d:%d\n",tminfo->tm_year+1900,tminfo->tm_mon+1,tminfo->tm_mday
    ,tminfo->tm_hour,tminfo->tm_min,tminfo->tm_sec);
    //system("pause");
    return 0;
}

总结:

相关文章:

  • Insar 相位展开真实的数据集的生成与下载(随机矩阵放大,zernike 仿真包裹相位)
  • 【编译原理】期末
  • 多表连接查询:语法、注意事项与最佳实践
  • Java+Python智能化网盘【Day9-1】
  • 趣味数据结构之——栈
  • 1 Studying《Computer Vision: Algorithms and Applications 2nd Edition》1-5
  • 基于springboot的海产品交易系统
  • AI代码助手实践指南
  • 03.BUG
  • (线性代数)矩阵的奇异值Singular Value
  • 测量 Linux 中进程上下文切换需要的时间
  • python 项目利用uv管理python包依赖
  • 如何在 Ubuntu 上通过终端或在 VirtualBox 中安装 GCC
  • 大事件项目记录11-文章分类接口开发-删除文章分类
  • zookeeper Curator(3):Watch事件监听
  • c++学习(一、指针)
  • 14、ElasticSearch
  • Kioptrix Level1
  • Word之电子章制作——1
  • 计算机网络——概述