嵌入式开发记录-day12 文件目录
1、更改文件权限chmod() chmod, fchmod - change permissions of a file
#include <sys/stat.h> int chmod(const char *path, mode_t mode); int fchmod(int fd, mode_t mode);
2、修改权限chmod
1 #include <sys/stat.h> 2 #include <stdio.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 7 void main(int argc,char* argv[]) 8 { 9 int ret, fd; 10 if(argc < 3){ 11 printf("\n please input file path\n"); 12 return 1; 13 } 14 /// chmod test 15 ret = chmod(argv[1],0777); 16 if(ret < 0){ 17 printf("Please make sure file path\n); 18 return 1; 19 } 20 printf("chmod %s is succes\n",argv[1]); 21 22 /// fchmod test 23 fd = open(argv[2],O_WRRD|O_NOCTTY|O_NDELAY); 24 if(fd < 0){ 25 printf("Please make sure file path\n"); 26 return 1; 27 } 28 ret = fchmod(fd,0555); 29 if(fd < 0){ 30 printf("Please make sure file path\n"); 31 return 1; 32 } 33 printf("fchmod %s is success\n",argv[2]); 34 35 return 0; 36 37 }
3、获取当前目录getcwd() getcwd, getwd, get_current_dir_name - Get current working directory
相关函数 #include <unistd.h> char *getcwd(char *buf, size_t size); char *getwd(char *buf); // 过时了 char *get_current_dir_name(void);
// 需注意 定义好宏定义 编译库所需要 #define __USE_GNU
4、测试
1 #include <stdio.h> 2 #include <unistd.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 7 #define __USE_GNU 8 #define LENGTH 255 9 10 int main(void) 11 { 12 char pwd[LENGTH]; 13 char* wd; 14 15 if(!getcwd(pwd,LENGTH)){ // The getcwd() function copies an absolute pathname of the current working directory 16 to the array pointed to by buf 17 perror("getcwd"); 18 return 1; 19 } 20 printf("\n getcwd pwd is %s\n", pwd); 21 22 ///// getcwd test 23 wd = getwd(pwd); 24 if(!wd){ 25 perror("getwd"); 26 return 1; 27 } 28 printf("\n getwd pwd is %s \n", wd); 29 30 //// test get_current_dir_name 31 wd = get_current_dir_name(); 32 if(!wd){ 33 perror("get_current_dir_name"); 34 return 1; 35 } 36 printf("\n get_current_dir_name wd is %s\n", wd); 37 }
5、mkdir()创建目录 mkdir() attempts to create a directory named pathname
// 相关函数 #include <sys/stat.h> #include <sys/types.h> int mkdir(const char *pathname, mode_t mode);
6、mkdir()测试
1 #include <sys/stat.h> 2 #include <sys/types.h> 3 #include <stdio.h> 4 6 int main(int argc,char* argv[]) 7 { 8 int ret; 9 if(argc < 2){ 10 printf("\n please input file path\n"); 11 return -1; 12 } 13 ret = mkdir(argv[1],0777);// 创建文件夹 14 if(ret< 0){ 15 printf("mkdir %s failed\n"); 16 return -2; 17 } 18 printf("mkdir %s sucess\n",argv[1]); 19 20 return 0; 21 }
7、rmdir()删除目录 rmdir() deletes a directory, which must be empty.
// 相关函数 #include <unistd.h> int rmdir(const char *pathname);
rmdir()删除目录测试
1 #include <stdio.h> 2 #include <sys/stat.h> 3 #include <sys/types.h> 4 #include <unistd.h> 5 6 int main(int argc,char* argv[]) 7 { 8 int ret; 9 if(argc<2){ 10 printf("\nplease input file path\n"); 11 return -1; 12 } 13 ret = rmdir(argv[1]); // 删除文件目录 14 if(ret !=0){ 15 printf("please make sure file path\n"); 16 return -2; 17 } 18 printf("\n rmdir %s is success\n",argv[1]); 19 20 return 0; 21 }
8、chdir()改变当前目录 chdir, fchdir - change working directory
#include <unistd.h> int chdir(const char *path); int fchdir(int fd); // 完全等效,打开方式不同
1 #include <unistd.h> 2 #include <stdio.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 7 #define LENGTH 255 8 9 int main(int argc,char* argv[]) 10 { 11 int ret,fd; 12 char pwd[LENGTH]; 13 14 if(argc < 2){ 15 printf("\n please input file path\n"); 16 return -1; 17 } 18 // 获取当前目录 19 if(!getcwd(pwd,LENGTH)){ 20 perror("getcwd"); 21 return -2; 22 } 23 printf("\n getcwd pwdis %s\n",pwd); 24 // 转入其他目录 25 ret= chdir(argv[1]); 26 if(ret != 0){ 27 printf(" please make sure file path\n"); 28 return -3; 29 } 30 printf("chdir: %s success\n",argv[1]); 31 return 0; 32 }
9、opendir() 打开目录 opendir, fdopendir - open a directory
#include <sys/types.h> #include <dirent.h> DIR *opendir(const char *name); DIR *fdopendir(int fd);
10、测试
1 #include <sys/types.h> 2 #include <dirent.h> 3 #include <stdio.h> 4 5 int main(int argc,char* argv[]) 6 { 7 int ret; 8 DIR* dir; 9 if(argc < 2){ 10 printf("\nplease input file path\n"); 11 return -1; 12 } 13 dir = opendir(argv[1]); 14 if(dir == NULL){ 15 printf("\n please make sure file path\n"); 16 return -2; 17 } 18 printf("\n opendir %s success\n",argv[1]); 19 closedir(dir); 20 printf("closedir success\n"); 21 22 return 0; 23 }
11、readdir() 读取文件夹基本信息 readdir, readdir_r - read a directory
#include <dirent.h> struct dirent *readdir(DIR *dirp); int readdir_r(DIR *dirp, struct dirent *entry, struct dirent **result);
struct dirent { ino_t d_ino; /* inode number */ off_t d_off; /* offset to the next dirent */ unsigned short d_reclen; /* length of this record */ unsigned char d_type; /* type of file; not supported by all file system types */ char d_name[256]; /* filename */ };
12、测试
1 #include <sys/types.h> 2 #include <dirent.h> 3 #include <stdio.h> 4 5 int main(int argc,char* argv[]) 6 { 7 int ret; 8 DIR* dir; 9 struct dirent* catlog; 10 if(argc < 2){ 11 printf("\nplease input file path\n"); 12 return -1; 13 } 14 dir = opendir(argv[1]); 15 if(dir == NULL){ 16 printf("\n please make sure file path\n"); 17 return -2; 18 } 19 catlog = readdir(dir); 20 if(catlog == NULL){ 21 printf("\n readdir %s failed\n",argv[1]); 22 return -3; 23 } 24 // 索引结点 ls -i 25 printf("\n%s d_ino is %ld\n",argv[1],catlog->d_ino); 26 closedir(dir); 27 28 return 0; 29 }
13、mycopy() 拷贝文件 在linux下没有专门的拷贝函数 需自己实现
1 #include <unistd.h> 2 #include <stdio.h> 3 #include <sys/types.h> 4 #include <sys/stat.h> 5 #include <fcntl.h> 6 #include <stdlib.h> 7 #include <string.h> 8 9 10 #define LENGTH 1024 11 // argv[1] oldpath 12 int main(int argc,char* argv[]) 13 { 14 int fd,fds; 15 char buf[LENGTH]; 16 char* fileold,*filenew; 17 18 fileold = argv[1]; 19 filenew = argv[2]; 20 21 if(argc < 3){ 22 printf("\n please input file path\n"); 23 return -1; 24 } 25 fds = open(argv[1],O_RDWR); 26 if(fds < 0){ 27 printf("\n please make sure file path\n"); 28 return -2; 29 } 30 fd = open(argv[2],O_RDWR|O_CREAT); 31 if(fd < 0){ 32 printf("\n please make sure file path\n"); 33 return -3; 34 } 35 // 读写操作 36 while(read(fds,buf,LENGTH)){ 37 write(fd,buf,strlen(buf)); 38 } 39 40 // 关闭文件 41 close(fd); 42 close(fds); 43 printf("\n cp finished\n"); 44 printf("\n cp %s to %s success\n",argv[1],argv[2]); 45 46 return 0; 47 }
14、重命rename() rename - change the name or location of a file
#include <stdio.h> int rename(const char *oldpath, const char *newpath);
15、测试
1 #include <stdio.h> 2 // 将一个文件名 修改另一个文件名 3 4 int main(int argc,char* argv[]) 5 { 6 int ret; 7 8 9 if(argc < 3){ 10 printf("\n please input file path\n"); 11 return -1; 12 } 13 ret = rename(argv[1],argv[2]); 14 if(ret != 0){ 15 printf("\n rename error\n"); 16 return -2; 17 } 18 printf("\n rename %s to %s success\n",argv[1],argv[2]); 19 20 return 0; 21 }
16、软硬连接相关
#include <unistd.h> int link(const char *oldpath, const char *newpath); // link - make a new name for a file int symlink(const char *oldpath, const char *newpath); // symlink - make a new name for a file int unlink(const char *pathname); // unlink - delete a name and possibly the file it refers to
硬链接
#include <unistd.h> #include <stdio.h> int main(int argc,char* argv[]) { int ret; if(argc < 3){ printf("\nplease input file path\n"); return -1; } ret = link(argv[1],argv[2]); if(ret){ printf("link failed\n"); return -2; } printf("link %s to %s success\n",argv[1],argv[2]); return 0; }
软连接
1 #include <unistd.h> 2 #include <stdio.h> 3 4 int main(int argc,char* argv[]) 5 { 6 int ret; 7 8 if(argc < 3){ 9 printf("\nplease input file path\n"); 10 return -1; 11 } 12 ret = symlink(argv[1],argv[2]); 13 if(ret){ 14 printf("link failed\n"); 15 return -2; 16 } 17 printf("symlink %s to %s success\n",argv[1],argv[2]); 18 19 return 0; 20 }
解除连接
#include <unistd.h> #include <stdio.h> int main(int argc,char* argv[]) { int ret; if(argc < 2){ printf("\nplease input file path\n"); return -1; } ret = unlink(argv[1]); if(ret){ printf("unlink failed\n"); return -2; } printf("unlink %s success\n",argv[1]); return 0; }