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

嵌入式自学第二十六天(5.22)

(1)文件查找:lseek
例:
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int argc, char **argv)
{

  int fd = open("1.txt",O_WRONLY|O_CREAT|O_TRUNC,0666);
  if(-1 == fd)
  {
    printf("open error");
    return 1;
  }

  off_t offset = lseek(fd,1024*1024*10,SEEK_SET);
  printf("%ld\n",offset);
  write(fd,"a",2);
  close(fd);
  //system("pause");
  return 0;
}
注意:第三个参数跟fseek一样
(2)文件流转文件描述符:fileno
例:

#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);
    //system("pause");
    return 0;
}

注意:关闭文件采用fclose,因为封装的比没封的功能全一些。
(3)文件描述符转文件流:fdopen
例:

#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);


    //system("pause");
    return 0;
}
(4)错误定位及原因打印:
#include <stdio.h>
#include <errno.h>  // extern int errno;

int    main(int argc, char **argv)
{
    perror("aaa");


    FILE* fp = fopen("10.txt","r");
    if(NULL == fp)
    {
        printf("errno %d\n",errno);
        perror("fopen main.c:10");
        return 0;

    }


    //system("pause");
    return 0;
}
注意:int errno 错误号,系统定义的变量
    perror(“提示”)出错后,提示符后面自动加上原因
(5)目录操作:opendir、readdir、closedir
例:
#include <dirent.h>
#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;
          default:
            printf("其他文件\n");
        }
    }
  closedir(dir);
  // system("pause");
  return 0;
}

注意:目录流DIR* dir指向目录
    linux文件类型:dir.type
man readdir
DT_BLK   block device 块设备,存储类b
字符设备 DT_CHR 鼠标 键盘c
DT_DIR 目录d
DT_FIFO 管道 pipe  p         makefifo pipe  ll
DT_LNK 符号链接,软链接 l
DT_REG  规则文件 普通
DT_SOCK 网络文件
    命令ll -i 得文件编号inode
(6)时间函数:
man 3 time
从1970年统计总秒数
time_t a = time(time_t *tloc)传NULL 或者long返回秒数

man 3 localtime
1900年开始记统计年数,t.year
struct tm * t = localtime(const char *)

例:
#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;
}

相关文章:

  • C#中WSDL文件引用问题
  • 爱博精电正式入驻京东平台,为客户提供更高效、便捷的采购体验
  • RocketMQ 中的 ConsumeQueue:消息消费的关键索引
  • Java单例模式终极指南:从原理到防御性编程
  • Python描述统计分析
  • PTA刷题笔记
  • 变上限积分是被积函数的一个原函数
  • 多用户批发商城系统哪个好?商淘云S2B2b多供应商批发源码评测
  • redis数据持久化和配置-15(备份和还原 Redis 数据)
  • 《棒球百科》棒球运动规则·棒球1号位
  • 3 PID控制学习指南
  • 密码分析学:从理论框架到实战攻防的全维度解析
  • 一文讲透:如何用AI生成时序图
  • springboot使用jdk17快速搭建教程
  • Vue2 项目报错问题收录(持续更新...)
  • 互联网大厂Java求职面试:企业知识库与AI大模型深度融合架构
  • 卷积神经网络基础(十一)
  • Python 实现web请求与响应
  • 钢条切割(动态规划)
  • gcc版本选择和软链接gcc
  • 做网站可以用.cn域名吗/网站 推广
  • 旅游行业网站建设/seo首页排名优化
  • 用dw做网站的菜单栏/优化模型有哪些
  • 网站建设与管理题库/关键词林俊杰无损下载
  • 怎么做网站里面的模块/今天发生的重大新闻5条
  • 网页设计尺寸单位一般为/南京 seo 价格