zoukankan      html  css  js  c++  java
  • 操作系统实验5 linux文件操作

    利用Linux有关系统调用函数编写一个文件工具filetools,要求具有下列功能:

      1 #include<stdio.h>
      2 #include<sys/types.h>
      3 #include<unistd.h>
      4 #include<fcntl.h>
      5 #include<sys/stat.h>
      6 #include<syslog.h>
      7 #include<string.h>
      8 #include<stdlib.h>
      9 #include<dirent.h>
     10 void menu();//选择菜单
     11 void createfile();// 创建文件
     12 void readfile();//读取文件
     13 void writefile();//写文件
     14 void copyfile();//复制文件
     15 void chmd();//修改文件权限
     16 void dispmode();//查看文件权限
     17 void createdir();//创建目录
     18 void lsdir();//查看目录
     19 void dedir();//删除目录
     20 void cd();//切换当前目录
     21 void lnfile();//建立符号链接
     22 
     23 void menu()
     24 {
     25     printf("********************************
    ");
     26     printf("*******   filetools       ******
    ");
     27     printf("******    0.退出           *****
    ");
     28     printf("*****     1.创建新文件      ****
    ");
     29     printf("*****     2.写文件          ****
    ");
     30     printf("*****     3.读文件          ****
    ");
     31     printf("*****     4.复制文件        ****
    ");
     32     printf("*****     5.修改文件权限    ****
    ");
     33     printf("*****     6.查看文件权限    ****
    ");
     34     printf("*****     7.创建目录        ****
    ");
     35     printf("*****     8.查看目录        ****
    ");
     36     printf("*****     9.删除目录        ****
    ");
     37     printf("******    10.切换当前目录  *****
    ");
     38     printf("*******   11.建立符号链接 ******
    ");
     39     printf("********************************
    ");
     40 }
     41 void createfile(char *file)//创建文件
     42 {
     43     int fd;
     44     if((fd = open(file,O_CREAT|O_TRUNC|O_RDWR,0666))<0)//已存在文件即可打开,未存在文件则创建文件
     45         perror("open");
     46     else
     47         printf("创建文件成功!
    ");
     48     if(close(fd))//判断文件是否关闭
     49         perror("close");
     50     else
     51         printf("Close file!
    ");
     52 }
     53 
     54 
     55 void readfile(char *file)
     56 {
     57     int fd,size;
     58     char b[105];
     59     if((fd = open(file,O_RDONLY))<0)//判断是否打开
     60         perror("open");
     61     else
     62         printf("Open file!");
     63     if((size = read(fd,b,sizeof(b)))<0)//判断是否读成功
     64         perror("read");
     65     else
     66         printf("read from file:%s
    ",b);
     67     if(close(fd)<0)//判断文件是否关闭
     68         perror("close");
     69     else
     70         printf("Close file!
    ");
     71 }
     72 void writefile(char *file)//写文件
     73 {
     74     int fd,size,len;
     75     char *buf="Hello!Iam lin!!!";//写文件的内容
     76     len = strlen(buf);
     77     if((fd = open(file,O_CREAT|O_TRUNC|O_RDWR,0666))<0)//判断是否打开
     78         perror("open");
     79     else
     80         printf("Open file!
    ");
     81     if((size = write(fd,buf,len))<0)//判断是否写入成功
     82         perror("write");
     83     else
     84         printf("Write:%s
    ",buf);
     85     if(close(fd)<0)//判断文件是否关闭
     86         perror("close");
     87     else
     88         printf("Close file!
    ");
     89 }
     90 
     91 void copyfile(char *file, char *p)//传入文件和待拷贝文件
     92 {
     93     if(fork()==0)
     94         execlp("/bin/cp","cp",file,p,NULL);
     95     else
     96         wait(0);
     97     printf("将%s文件内容复制到%s
    ",file,p);
     98 }
     99 
    100 void chmd(char *filename)//复制实验书上代码
    101 {
    102     int c;
    103     mode_t mode=S_IWUSR;//mode值
    104     printf(" 0. 0700
     1. 0400
     2. 0200 
     3. 0100
    "); //还可以增加其它权限
    105     printf("Please input your choice(0-3):");
    106     scanf("%d",&c);
    107     switch(c)
    108     {
    109     case 0:
    110         chmod(filename,S_IRWXU);
    111         break;
    112     case 1:
    113         chmod(filename,S_IRUSR);
    114         break;
    115     case 2:
    116         chmod(filename,S_IWUSR);
    117         break;
    118     case 3:
    119         chmod(filename,S_IXUSR);
    120         break;
    121     default:
    122         printf("You have a wrong choice!
    ");
    123     }
    124 }
    125 
    126 
    127 void dispmode(char *filename)//查看文件权限
    128 {
    129     char* path="/bin/ls";
    130     char* argv[4]= {"ls","-l",filename,NULL};
    131     execv(path,argv);    //执行ls –l file1
    132 }
    133 
    134 
    135 void createdir(char *file)//创建文件夹
    136 {
    137     if(mkdir(file,S_IRWXU)<0)
    138         perror("Mkdir");
    139     else
    140         printf("创建文件夹成功
    ");
    141 }
    142 
    143 
    144 
    145 void lsdir(char *dir, int depth)//递归查看目录下的文件,参考百度
    146 {
    147     DIR *dp;
    148     struct dirent *entry;
    149     struct stat statbuf;
    150     //结构体
    151 
    152     if ((dp = opendir(dir)) == NULL)//判断是否能打开文件
    153     {
    154         fprintf(stderr, "Can`t open directory %s
    ", dir);
    155         return ;
    156     }
    157 
    158     chdir(dir);
    159     while ((entry = readdir(dp)) != NULL)
    160     {
    161         lstat(entry->d_name, &statbuf);
    162         if (S_ISDIR(statbuf.st_mode))
    163         {
    164             if (strcmp(entry->d_name, ".") == 0 ||
    165                     strcmp(entry->d_name, "..") == 0 )
    166                 continue;
    167             printf("%*s%s/
    ", depth, "", entry->d_name);
    168             lsdir(entry->d_name, depth+4);//递归式查找
    169         }
    170         else
    171             printf("%*s%s
    ", depth, "", entry->d_name);//输出目录下文件
    172     }
    173     chdir("..");
    174     closedir(dp);
    175 }
    176 
    177 void dedir(char *path)
    178 {
    179     printf("不会递归写删除子目录
    ");
    180 }
    181 
    182 
    183 
    184 void cd(char *file)
    185 {
    186     if(chdir(file)<0)
    187         perror("chdir");
    188     else
    189         printf("切换目录成功
    ");
    190 }
    191 
    192 void lnfile(char *a, char *b)
    193 {
    194     if(link(a,b) < 0)
    195         perror("Link");
    196     else
    197         printf("已建立连接
    ");
    198 }
    199 
    200 int main()
    201 {
    202     //打印菜单
    203     menu();
    204     int num;
    205     printf("请选择您要操作的内容的序号:");
    206     while(1)
    207     {
    208         scanf("%d", &num);
    209         if(num == 0)
    210         {
    211             printf("已退出
    ");
    212             return 0;
    213         }
    214         else if(num > 0 && num <= 11)
    215         {
    216             switch(num)
    217             {
    218             case 1:
    219                 createfile("/home/xmonkey/3.txt");
    220                 break;
    221             case 2:
    222                 writefile("/home/xmonkey/hello.txt");
    223                 break;
    224             case 3:
    225                 readfile("/home/xmonkey/hello.txt");
    226                 break;
    227             case 4:
    228                 copyfile("/home/xmonkey/hello.txt","/home/xmonkey/hello.c");
    229                 break;
    230             case 5:
    231                 chmd("/home/xmonkey/hello.txt");
    232                 break;
    233             case 6:
    234                 dispmode("/home/xmonkey/hello.txt");
    235                 break;
    236             case 7:
    237                 createdir("/home/xmonkey/3");
    238                 break;
    239             case 8:
    240                 lsdir("/home/xmonkey/file",0);
    241                 break;
    242             case 9:
    243                 dedir("/home/xmonkey/file");
    244                 break;
    245             case 10:
    246                 cd("/home/xmonkey/file");
    247                 break;
    248             case 11:
    249                 lnfile("/home/xmonkey/hello.txt","/home/xmonkey/3.txt");
    250                 break;
    251 
    252             }
    253         }
    254         else
    255         {
    256             printf("序号出错,请重新输入!
    ");
    257         }
    258     }
    259 
    260 }
  • 相关阅读:
    luogu 1865 数论 线性素数筛法
    洛谷 2921 记忆化搜索 tarjan 基环外向树
    洛谷 1052 dp 状态压缩
    洛谷 1156 dp
    洛谷 1063 dp 区间dp
    洛谷 2409 dp 月赛题目
    洛谷1199 简单博弈 贪心
    洛谷1417 烹调方案 dp 贪心
    洛谷1387 二维dp 不是特别简略的题解 智商题
    2016 10 28考试 dp 乱搞 树状数组
  • 原文地址:https://www.cnblogs.com/1998LJY/p/10860075.html
Copyright © 2011-2022 走看看