"); //-->
#include <unistd.h>int chdir(const char *path);
二、getcwd 函数
#include <unistd.h> char *getcwd(char *buf, size_t size); char *getwd(char *buf);
#include<unistd.h>
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<stdlib.h>
#include<stdio.h>
int main(int argc, char *argv[] )
{
if( argc<2 )
{ perror("./a.out filepath");
exit(1); }
printf(" agrv[1] = %s\n",argv[1]); // 修改当前的路径 int ret =chdir(argv[1]);
if( ret == -1 )
{
perror("chdir");
exit(1);
} // 在这里通过在改变后的目录下创建一个新的文件,来证明目录已经改变 int fd = open("chdir.txt",O_CREAT|O_RDWR,0644);
if( fd == -1 ) { perror("open"); exit(1); } close(fd); // 获取改变目录后的目录名 char buf[100]={0}; getcwd(buf,sizeof(buf));
printf("current dir: %s\n",buf); return 0;}#include <unistd.h> int rmdir(const char *pathname);
四、mkdir 函数
#include <sys/stat.h> #include <sys/types.h> int mkdir(const char *pathname, mode_t mode);
五、opendir 函数
#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd);
3、返回值:
#include <dirent.h> struct dirent *readdir(DIR *dirp);
3、返回值:
struct dirent {ino_t d_ino; /* inode number */ // 目录进入点的 inodeoff_t d_off; /* not an offset; see NOTES */ // 目录文件头开始至此目录进入点的位移unsigned short d_reclen; /* length of this record */ // d_name 长度unsigned char d_type; /* type of file; not supported // d_name 所指的文件夹 by all filesystem types */char d_name[256]; /* filename */ // 文件名};d_tyep 有 8 种类型:(1)、 DT_BLK This is a block device. 块设备(2)、 DT_CHR This is a character device. 字符设备(3)、 DT_DIR This is a directory. 目录(4)、 DT_FIFO This is a named pipe (FIFO). 管道(5)、 DT_LNK This is a symbolic link. 软链接(6)、 DT_REG This is a regular file. 普通文件(7)、 DT_SOCK This is a UNIX domain socket. 套接字(8)、 DT_UNKNOWN The file type is unknown. 未知类型#include <sys/types.h> #include <dirent.h> int closedir(DIR *dirp);
3、返回值:
#include<unistd.h>#include<dirent.h>#include<sys/types.h>#include<sys/stat.h>#include<stdlib.h>#include<stdio.h>#include<string.h>// 获取 root 目录下的文件个数int get_file_count(char *root){ // open dir DIR * dir = NULL; dir = opendir(root); if( NULL == dir ) { perror("opendir"); exit(1); } // 遍历当前打开的目录 struct dirent* ptr = NULL; char path[1024]={0}; int total = 0; while( (ptr = readdir(dir) )!= NULL) { // 过滤掉 . 和 .. if( strcmp(ptr->d_name,".") == 0 || strcmp(ptr->d_name,"..") == 0 ) { continue; } // 如果是目录,递归读目录 if(ptr->d_type == DT_DIR) { sprintf(path,"%s/%s",root,ptr->d_name); total += get_file_count(path); } // 如果是普通文件 if( ptr->d_type == DT_REG ) { total++; } } // 关闭目录 closedir(dir); return total;}int main(int argc,char *argv[]){ if( argc<2 ) { perror("./a.out dir\n"); exit(1); } // 获取文件个数 int count = get_file_count(argv[1]); printf("%s has file numbers : %d\n",argv[1],count); return 0;}#include <unistd.h> int dup(int oldfd); int dup2(int oldfd, int newfd);
3、返回值:
#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h>int main(void){ int fd =open("a.txt",O_RDWR); if( fd == -1 ) { perror("open"); exit(1); } printf("file open fd = %d\n",fd); // 找到进程文件描述符表 ======= 第一个========== 可用的文件描述符 // 将参数指定的文件复制到该描述后 返回这个描述符 int ret = dup(fd); if( fd == -1 ) { perror("dup"); exit(1); } printf(" dup fd = %d\n",ret); char *buf = "你是猴子请来的救兵吗??\n"; char *buf1 = "你大爷的,我是程序猿!!!\n"; write(fd,buf,strlen(buf)); write(ret,buf1,strlen(buf1)); close(fd); return 0;}#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h>int main(void){ int fd =open("english.txt",O_RDWR); if( fd == -1 ) { perror("open"); exit(1); } int fd1 =open("a.txt",O_RDWR); if( fd1 == -1 ) { perror("open"); exit(1); } printf("fd = %d\n",fd); printf("fd1 = %d\n",fd1); int ret = dup2(fd1, fd); if( ret == -1 ) { perror("dup2"); exit(1); } printf(" current fd = %d\n",ret); char *buf = "主要看气质 !!!!!!!!!!!!!!!!!\n"; write(fd,buf,strlen(buf)); write(fd1,"hello world!",12); close(fd); close(fd1); return 0;}#include <unistd.h> #include <fcntl.h> int fcntl(int fd, int cmd, ... /* arg */ ); 这是一个可变长参数的函数
3、功能:
#include<unistd.h>#include<sys/types.h>#include<sys/stat.h>#include<fcntl.h>#include<stdio.h>#include<stdlib.h>#include<string.h>int main(void){ int flag; int fd; // 测试字符串 char *p = "我们是一个由中国特使社会主义的国家!!!!!"; char *q ="呵呵,社会主义好哇"; // 以只写方式打开文件 fd = open("test.txt",O_WRONLY); if( fd == -1 ) { perror("open"); exit(1); } // 输入新的内容,该内容会覆盖原来的内容 if( write(fd,p,strlen(p)) == -1 ) { perror("write"); exit(1); } // 使用 F_GETFL 命令得到文件状态标志 int flags = fcntl(fd,F_GETFL,0); if( flags == -1 ) { perror("fcntl"); exit(1); } // 将文件状态标志添加 “追加写” 选项 flag |= O_APPEND; // 将文件状态修改为追加写 if( fcntl(fd,F_SETFL,flag) == -1 ) { perror("fcntl"); exit(1); } // 再次输入新的内容,该内容会追加到旧内容对的后面 if( write(fd,q,strlen(q)) == -1 ) { perror("write again"); exit(1); } return 0;}小结:
https://blog.csdn.net/qq_35396127/article/details/78543610
专栏文章内容及配图由作者撰写发布,仅供工程师学习之用,如有侵权或者其他违规问题,请联系本站处理。 联系我们
相关推荐
英飞凌扩展其CoolSiC™产品系列,推出专为高功率与计算密集型应用而设计的400V和440V MOSFET
基于PIC16F877单片机的电子秤包装机控制系统
Imec 开设海尔布隆中心
电压负反馈偏置电路
NCL30000单段式功率因数校正LED驱动器原理及TRIAC调光LED驱动器设计方案(下)
中国软件商机可分为“金角银边草肚皮
双端对称输入差动放大器电路
研华PCIe Gen5 x4 SSD EDSFF数据中心解决方案
NCL30000单段式功率因数校正LED驱动器原理及TRIAC调光LED驱动器设计方案(上)
JEDEC标准针对人工智能数据中心的LPDRAM模块
基于PSD的单片机典型应用系统的硬件设计
基于PPP协议单片机拔号上网的设计与实现
分压式电流负反馈偏置电路
迎接广播电视的数字时代(上)
基于PIC16C72单片机的空调控制系统的研制
固定偏置放大电路
巴斯夫与中国石化达成化工产品碳足迹核算方法学互认
利用基于氮化镓的解决方案为下一代 800 伏直流 AI 数据中心提供动力
基于PIC单片机的热能表研制
微软输掉官司 Windows必须嵌Java
中汽创智重磅亮相SAECCE 2025,行业首发“人-车-路-云-星”全维信息技术平台联合创新体
采用ADP1043A的数字电源设计实例
迎接广播电视的数字时代(下)
电源产品的可靠性设计(中)
北斗天通+电子地图技术加持 大理州筑牢文旅户外保障体系
电源产品的可靠性设计(上)
Arm力推面向汽车的小芯片标准
FD-SOI:用于安全汽车电子的网络弹性基板
单端输出差动放大器电路
东山再起的无线局域网(WLAN)