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

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

http://www.xdnf.cn/news/8296.html

相关文章:

  • 26-RTOS的基本介绍
  • Vitis 2021.1安装步骤
  • oracle数据库查看当前session会话数、查询最大支持连接数、修改最大支持连接数
  • SDL文字库介绍和RV1126交叉编译SDL文字库
  • 【Java】CompletableFuture.orTimeout在JDK21以下版本有内存泄漏及解决方案
  • 软件设计师“IP协议与子网划分”真题考点分析——求三连
  • Docker安装postgres数据库
  • Vue.js教学第十章:自定义命令的创建使用与应用
  • python——多线程编程(threading)
  • 【工具】Quicker/VBA|PPT 在指定位置添加有颜色的参考线
  • 交叉编译DirectFB报错解决方法
  • Day 28 训练
  • nginx和docker常用指令
  • thinkpad x220降频到0.7Ghz解决办法
  • 遥控器芯片6nm与3nm技术对比!
  • 如何将吉客云奇门数据无缝对接金蝶云星空
  • 远程升级方案(经通讯管理机)
  • 怎么有效降低知网AIGC率?
  • 华为RH1288HV3 BMC忘记用户名密码如何恢复默认
  • 华为仓颉语言如何进行交叉编译
  • 第三十二天打卡
  • 构建基于全面业务数据的大数据与大模型企业护城河战略
  • 【Linux】网络基础3
  • JUC入门(五)
  • 进程、线程和协程切换的比喻
  • 【分治】归并排序:递归版 非递归版
  • IDC机房交换机紧急更换的流程和注意事项
  • torch.gather()和torch.sort
  • 火语言UI组件--控件函数调用
  • 免费开源的图片分割小工具