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

Linux系统编程-DAY03

一、部分文件io和文件夹相关函数

1.lseek函数: off_t lseek(int fd, off_t offset, int whence)

2.用od -t x1(十六进制)文件名    看二进制文件

3.fopen  open  (补充)
w      O_WRONLY | O CREAT |O_TRUNC
w+    O_RDWR | O_CREA | O_TRUNC 
r        O_RDONLY
r+      O_RDWR
a       O_WRONLY | O_CREAT | O_APPEND
a+     O_RDWR | O_CREAT | O_APPEND

4. fdopen
FILE *fdopen(int fd, const char *mode)
关闭文件用fclose,封装度高

5.fileno函数: 

6.perror()系统性报错的函数
参数里输入标识性信息eg:(“fopen main.c :10”)
要包<errno.h>头文件     适用范围:man2 man3

7.目录:(1)打开opendir(2)读取目录readdir (3)关闭closedir
-------DIR *dir-------  (系统结构体)目录流指针
--------direntry *info---------
    struct dirent *readdir(DIR *dirp)
    包头文件#include<dirent.h>

8.把存储设备称为块设备,linux底层逻辑中,大致分为块设备和字符设备
DT_BLK 块设备     DT_CHR 字符设备
DT_DIR 目录、     DT_FIFO  管道。
windows桌面上放的东西叫做快捷方式,在liunx中叫做符号链接或软链接(DT_LNK)
DT_REG普通文件、DT_SOCK网络文件

9.  time_t time (time _ t *tloc)     头文件<time.h>
man 3 localtime    
struct tm *localtime(const time_t *time)

 二、例题与练习
1.lseek函数

#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
int main(int arcg, char **argv)
{int fd = open("1.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);if(-1 == fd){return 1;}off_t offset = lseek(fd, 1024 * 1024 * 10, SEEK_SET);printf("%ld\n", offset);write(fd, "b", 5);close(fd);return 0;
}

2.fileno.c函数

#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);fclose(fp);return 0;
}

3.fdopen.c函数


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

4.perror.c函数

#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;}return 0;
}

5.time.c函数

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

6./proc函数,计算数字文件的个数

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>int	main(int argc, char **argv)
{DIR *dir = opendir("/proc");int count = 0;while(1){struct dirent *info = readdir(dir);if(NULL == info){break;}if(info->d_name[0] > 48  && info->d_name[0] <= 57){count++;}}printf("number file is %d\n", count);closedir(dir);return 0;
}

7.文件ls的递归

#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>void open(char s[1024])
{DIR *dir = opendir(s);while(1){struct dirent *info = readdir(dir);if(NULL == info){break;}if(strcmp(info->d_name, "..") == 0 || strcmp(info->d_name, ".") == 0){continue;}else if (info->d_type == DT_DIR && strcmp(info->d_name, "..") && strcmp(info->d_name, ".")) {char s1[1024 * 1024];printf("----------------------------\n");strcpy(s1, s);strcat(s1, "/");strcat(s1, info->d_name);open(s1);}else{printf("%s\n", info->d_name);}}closedir(dir);
}int	main(int argc, char **argv)
{open("/home/linux/Linux biancheng");//system("pause");return 0;
}

相关文章:

  • 新一代网校培训平台的技术演进:从教育源码开发到AI赋能教学转型
  • 【笔试强训day38】
  • 有没有开源的企业网盘,是否适合企业使用?
  • ssrf漏洞学习
  • labview实现登录系统
  • PyTorch中diag_embed和transpose函数使用详解
  • 工商业预付费系统组成架构及系统特点介绍
  • 01-jenkins学习之旅-window-下载-安装-安装后设置向导
  • Spring IoC 和 AOP -- 核心原理与高频面试题解析
  • 设计双向链表--LeetCode
  • MinerU教程第二弹丨MinerU 本地部署保姆级“喂饭”教程
  • BGE-M3 文本情感分类实战:预训练模型微调,导出ONNX并测试
  • OpenCv高阶(十七)——dlib库安装、dlib人脸检测
  • Jeecg漏洞总结及tscan poc分享
  • Mujoco 学习系列(四)官方模型仓库 mujoco_menagerie
  • LangChain文档加载器实战:构建高效RAG数据流水线
  • 第八天的尝试
  • js中encodeURIComponent函数使用场景
  • 3.9/Q1,GBD数据库最新文章解读
  • FinalShell 密码在线解析方法(含完整源码与运行平台)
  • 一等一网站建设/济南seo小黑seo
  • 四川做网站设计哪家好/seo 优化 服务
  • 手把手教你优化网站/2024年的新闻
  • 哪个网站可以看一级a做爰片t/百度竞价是什么意思
  • 个人网站 模版 后台管理系统/seo助手
  • 建设部网站燃气管理部门/百度投诉中心在线申诉