Linux高级程序设计之文件系统编程课后练习

Linux高级程序设计->文件系统编程课后练习

  1. 文件操作练习一

编写代码,完成以下功能:

1.创建文件file1,写入字符串“abcdefghijklmn”;

2.创建文件file2,写入字符串“ABCDEFGHIJKLMN”;

3.读取file1中的内容,写入file2,使file2中的字符串内容为“abcdefghijklmn ABCDEFGHIJKLMN”



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<errno.h>

#include<string.h>

#include<fcntl.h>

#include<sys/types.h>

int main()

{

     int fd1,writenum,fd2;

     fd1 = open("file1.txt",O_RDWR|O_CREAT);

     if(fd1 < 0){

          perror("open file1:");

          printf("error is %d\n",errno);

     }

     else

          printf("open file1 OK\n");

     char* str="abcdefghijklmn";

     writenum = write(fd1,str,strlen(str));

     if(writenum != strlen(str)){

          perror("write file1:");

          printf("error is %d\n",errno);

     }

     else

          printf("write OK\n");

     char buf[255];

     fd2=open("file2.txt",O_RDWR|O_CREAT);

      if(fd2 < 0){

          perror("open file2:");

          printf("error is %d\n",errno);

     }

     else

          printf("open file2 OK\n");

      lseek(fd2,15,SEEK_SET);

       writenum = write(fd2,"ABCDEFGHIJKLMN",14);

     if(writenum != 14 ){

          perror("write file2:");

          printf("error is %d\n",errno);

     }

       else

          printf("write file2 OK\n");

      lseek(fd1,0,SEEK_SET);

      lseek(fd2,0,SEEK_SET);

      read(fd1,buf,5);

      write(fd2,buf,5);

     close(fd1); 
     close(fd2);

     return 0;     

}

  1. 文件操作练习二

    编写代码,完成以下功能:

    1.创建新文件,该文件具有用户读写权限。

    2.采用dup/dup2/fcntl复制一个新的文件描述符,通过新文件描述符向文件写入“class_name”字符串;

    3.通过原有的文件描述符读取文件中的内容,并且打印显示;



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<errno.h>

#include<fcntl.h>

Int main()

{

    int fd,fdcopy,writeNum,readNum;
    
    fd = open(“test.txt”,O_RDWR|O_CREAT);
    
    if(fd < 0){
    
          perror(“open:);
          
          printf(“error is : %d\n”,errno);
          

    }
    
    else
    
	printf(“open OK\n”);
	
    char str[] = “class_name”;
    
    fscopy=dup(fd);
    
    writeNum = write(fdcopy,str ,strlen(str));
    
    if(weriteNum != strlen(str)){
    
	perror(“writecopy:);
	
          printf(“error is : %d\n”,errno);
          
     }

     else
	
	printf(“write OK\n”);

     lseek(fd,0,SEEK_SET);

     char buf[]=“”;

     readNum = read(fd,buf,strlen(str));

     if(readNum != strlen(str)){

          perror(“read:);

          printf(“error is : %d\n”,errno);

     }

    else

     printf(“read Ok,the string is :%s\n”,buf);

   close(fd);

   close(fdcopy);

   return 0;

}

  1. 文件操作练习三

编写程序实现以下功能:

输入文件名称,能够判断文件类型,判断实际用户对该文件具有哪些存取权限;

要求打印出文件类型信息,inode节点编号,链接数目,用户id,组id,文件大小信息;

修改文件的权限为当前用户读写,组内用户读写,组外用户无权限。



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<errno.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>



int main(int argc,char* argv[])

{

     if(argc < 2){

     printf(“two or more permenters;\n”);

}
   
     struct stat statBuf;//复制文件状态

     char* str;//文件类型

     for(int i = 1; i < argc ;i++){

       printf(%s  message:\n”,argv[]);

     if(lstat(argv[i],&statBuf) < 0)

          perror(“latat error \n“);

     //判断文件类型

     if(S_ISREG(statBuf.st_mode))

          str = “regular file”;

     else if((S_ISDIR(statBuf.st_mode))

	str = “directory file”;
 
     else if((S_ISCHR(statBuf.st_mode))

	str =char device file”;

     else if((S_ISFIFO(statBuf.st_mode))

	str = “fifo file”;

     else if((S_ISBLK(statBuf.st_mode))

	str = “blk file”;

     else if((S_ISLNK(statBuf.st_mode))

	str = “symbolic link file”;

     else if((S_ISSOCK(statBuf.st_mode))

	str = “socket file”;

     printf(“ this file is a %s\n”,str);

//判断存取权限

     if(access(argv[i],F_OK) < 0)

     	perror(“file access error\n”);

     else{

     	if(access(argv[i],R_OK) < 0)

          perror(“access R_OK\n”);

     else

          printf(“user can read %s\n”,argv[i]);

     if(access(argv[i],W_OK) < 0)

          perror(“access W_OK\n”);

     else

          printf(“user can write %s\n”,argv[i]);

     if(access(argv[i],X_OK) < 0)

          perror(“access X_OK\n”);

     else

          printf(“user can run %s\n”,argv[i]);

   }

//打印文件信息

printf(%s message:\n”,argv[i]);

printf(“file type is %s;\n”,str);

printf(“inode is %ld;\n”,statBuf.st_ino);

printf(“linkNum is %ld;\n”,statBuf.st_nlink);

printf(“uid is %ld;\n”,statBuf.st_uid);

printf(“gid is %ld;\n”,statBuf.st_gid);

printf(“size is %ld;\n”,statBuf.st_size);

printf(------------------------\n");

//修改文件权限

     if(chmod(argv[i],0660) < 0)

	printf(“chmod error\n”);

}

return 0;

}

  1. 文件操作练习四

    编写程序实现以下功能:

    1.新建文件,设置文件权限屏蔽字为0;

    2.建立该文件的硬链接文件,打印硬链接文件的inode节点号和文件大小;

    3.建立该文件的软链接文件,打印软链接文件的inode节点号和文件大小;打印软链接文件中的内容;

    4.打印源文件的inode节点号,文件大小和链接数目;

    5.调用unlink对源文件进行操作,打印源文件链接数目;



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<errno.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

int main()

{

int fd1,fd2,fd3;

     struct stat statBuf1,statBuf2,statBuf3;

     fd1 = open(“file1”,O_RDWR|O_CREAT);

     if(fd1 < 0)

     {

          perror(“open:);

          printf(“error is :%d\n”.errno);

}

fchmod(fd1,S_IRWXU|S_IRWXG|S_IRWXO);

int writeNum;

writeNum = write(fd1,123456789,9);

if(writeNum != 9)

perror(“write error is:%d\n”,errno);

else

printf(“write OK\n”);

     umask(0);

     int link1 = link(“file1”,”file2”);

     if(link1 < 0)

          perror(“link1”);

     stat(“file2”,&statBuf2);

     printf(“hard link file inode :%ld\n”,statBuf2.st_ino);

     printf(“hard link file size :%ld\n”,statBuf2.st_size);

     int link2 = symlink(“file1”,”file3”);

     if(link2 < 0)

          perror(“link2”);

     lstat(“file3”,&statBuf3);

     char str[10];

     int readNum = readlink(“file3”,str,10);

     if(readNum < 0)

          perror(“read error :%d\n”,errno);

     else

          printf(“read OK\n”);

     printf(“symbolic link file inode :%ld\n”,statBuf3.st_ino);

     printf(“symbolic link file size :%ld\n”,statBuf3.st_size);

     printf(“symbolic link file content :%s\n”,str);

     fstat(fd1,&statBuf1);

     printf(“file inode :%ld\n”,statBuf1.st_ino);

     printf(“file size :%ld\n”,statBuf1.st_size);

     printf(“file link number :%d\n”,statBuf1.st_nlink);

     if(unlink(“file1”) < 0)

          perror(“unlink”);

     close(fd1);

     return 0;

}

  1. 文件操作练习五

1.新建/home/user目录;

2.把当前工作路径移至/home/user目录;

3.打印当前工作路径;



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<error.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<dirent.h>

int main()

{

     if(mkdir(/home/user”,0777) < 0)

          perror(“mkdir error:%d\n”,errno);

     char dir[] =/home/user”;

     chdir(dir);

     char dircopy[255];

     getcwd(dircopy,255);

     printf(“current directory is :%s\n”,dircopy);

     rmdir(/home/user”);

  
return 0;

}

  1. 文件操作练习六

编写程序完成以下功能:

1.递归遍历/home目录,打印出所有文件和子目录名称及节点号。

2.判断文件类型,如果是子目录,继续进行递归遍历,直到遍历完所有子目录为止。



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<error.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<dirent.h>

void scan(char* path)

{

     char str[255];

     DIR *dirpath;

     struct dirent *dir;

     struct stat statbuf;

     dirpath = openddir(path);

     if(dirpath)

{

     while((dir = readdir(dirpath)))

     {

     sprintf(str,%s%s”,path,dirpath->d_name);

     if(lstat(str,&statbuf) < 0)

          perror(“lstat error”);

     if(dir->d_name[0] ==.)

          continue;

     if(S_ISDIR(statbuf.st_mode))

          {

               scan(str);

            printf(“the dirent name is :%s\n”,dir->d_name);

               printf(“the dirent inode is :%d\n”,dir->d_ino);

          }

     else

     {

          printf(“the file name is :%s\n”,dir->d_name);

     printf(“the file inode is :%d\n”,dir->d_ino);

}

}

}

else

perror(“open dir”);

closedir(dir);

}

int main()

{

    scan(/home”);

     return 0;

}

  1. 作业

编写一个程序,提供一个参数(文件名/路径)

按照下面的要求修改文件权限

设置创建者拥有RWX权限,其他用户只有R-X权限

如果该文件是普通文件 读取并显示该文件的属性/状态信息并创建该文件的一个拷贝

如果该文件是目录文件 显示该目录中的内容及其子目录内容



#include<stdio.h>

#include<stdlib.h>

#include<unistd.h>

#include<string.h>

#include<error.h>

#include<fcntl.h>

#include<sys/types.h>

#include<sys/stat.h>

#include<dirent.h>

void scan(char* path)

{

     char str[255];

     DIR *dirpath;

     struct dirent *dir;

     struct stat statbuf;

     dirpath = openddir(path);

     if(dirpath)

{

     while((dir = readdir(dirpath)))

     {

     	sprintf(str,%s%s”,path,dirpath->d_name);
	
     	if(lstat(str,&statbuf) < 0)

        	  perror(“lstat error”);

     	if(dir->d_name[0] ==.)

      	    continue;

    	 if(S_ISDIR(statbuf.st_mode))

     	     {

       	        scan(str);

        	    printf(“the dirent name is :%s\n”,dir->d_name);

         	      printf(“the dirent inode is :%d\n”,dir->d_ino);

              }

    	 else

    	 {

      	    printf(“the file name is :%s\n”,dir->d_name);

       	   printf(“the file inode is :%d\n”,dir->d_ino);

   	  }
   
   }

}

else

perror(“open dir”);

closedir(dir);

}

int main(int argc,char* argv[])

{

    int fd;

     struct stat statBuf;

     char str[255];

for(int i = 0; i < argc; i++)

{

     if(stat(argv[i],&statBuf) < 0)

          perror(“stat error”);

     if(chmod(argv[i],S_IRWXU|S_IROTH|S_IXOTH) < 0)

          perror(“chmod error”);

     if(S_ISREG(statBuf.st_mode))

{

          printf(“this is a regular file\n”);

     printf(%s message:\n”,argv[i]);

printf(“inode is %ld;\n”,statBuf.st_ino);

printf(“linkNum is %ld;\n”,statBuf.st_nlink);

printf(“uid is %ld;\n”,statBuf.st_uid);

printf(“gid is %ld;\n”,statBuf.st_gid);

printf(“size is %ld;\n”,statBuf.st_size);

          FILE *fp1,*fp2;

          fp1 = fopen(argv[i],”r”);

          fp2 = fopen(“filecopy”,”w+);

          while(!feof(fp1))

          putc(getc(fp1),fp2);

          fclose(fp1);

          fclose(fp2);

}

else if(s_IFDIR(statBuf.st_mode))

scan(argv[i]);

else

     printf(“this not is a regular file or directory\n”);

}

     return 0;

}

发布了13 篇原创文章 · 获赞 14 · 访问量 2483

猜你喜欢

转载自blog.csdn.net/weixin_44929171/article/details/102525820