Linux实习报告—实验四 文件I/O操作

本文详细介绍了C语言中文件描述符操作、非阻塞I/O、文件锁、目录与符号链接的应用,包括新建文件权限设置、文件内容读写、链接操作及ls命令实现。通过实例演示了如何使用open(), chmod(), write(), read(), symlink()和link()等函数解决实际问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

目的

  1. 熟悉文件描述符操作文件的函数;
  2. 掌握非阻塞文件操作应用;
  3. 掌握文件锁应用;
  4. 掌握目录文件、符号链接文件的操作。

内容                                

  1. 教材第5章末,213页,1,2,3,4,7题。
  • 设计一个程序,要求打开文件“pass”,如果没有这个文件,新建此文件,权限设置为只有所有者有只读权限。
  • #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    int main()
    {
        int fd;
        if((fd=open("pass",O_CREAT)<0))
        {
            printf("file is not exist.\n");
        }
        printf("createFile pass,fd:%d\n",fd);
        chmod("pass",S_IRUSR);
        return 0;
    }

    2.设计一个程序,要求新建一个文件“hello”,利用write函数将“Linux下C软件设计”字符串写入该文件。

  • #include <stdio.h>
    #include <unistd.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/stat.h>
    #include <fcntl.h>
    #include <string.h>
    int main()
    {
    	int fd,w;
    	if((fd=open("hello",O_TRUNC|O_WRONLY,0666))<0)
    	{
    		printf("打开文件出错");
    		return 1;
    	}
    	char str[]="Linux下C软件设计";
    	if((w=write(fd,str,sizeof(str)))<0)
    	{
    		printf("写入文件出错");
    		return 1;
    	}
    	close(fd);
    	return 0;
    }
    

     3.设计一个程序,要求利用read函数读取系统文件“/etc/passwd”,并在终端中显示输出

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main()
{
	int fd,r,w;
	char str[50];
	if((fd=open("/etc/passwd",O_RDONLY))<0)
	{
		printf("读取文件失败!");
		return 1;
	}
	while((r=read(fd,str,50))>0)
	{
		if((w=write(STDOUT_FILENO,str,r))<0)
			printf("写入文件出错");
	}
	close(fd);
	return 0;
}

 设计一个程序,要求打开文件“pass”,如果没有这个文件,新建此文件,在读取系统文件“/etc/passwd”,把文件的内容都写入“pass”文件。

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int main()
{
	int fdsrc,fddes,cnt,w;
	char str[30];
	if((fddes=open("pass",O_CREAT|O_TRUNC|O_WRONLY,0600))<0)
	{
		printf("打开文件pass失败!");
		return 1;
	}
	if((fdsrc=open("/etc/passwd",O_RDONLY))<0)
	{
		printf("打开文件失败!");
		return 1;
	}
	while((cnt=read(fdsrc,str,30))>0)
	{
		if((w=write(fddes,str,30))<0)
			printf("写入文件失败");
	}
	close(fdsrc);
	close(fddes);
	return 0;
}


问题:一直没有找到为什么文件pass一直打开失败? 

⑦ 设计一个程序,要求为“/bin/ls”文件建立一个软链接“ls1”和一个硬链接“ls2”,并查看两个链接文件和“/bin/ls”文件。

#include <stdlib.h>
#include <unistd.h>
int main()
{
	symlink("/bin/ls","ls1");
	link("/bin/ls","ls2");
	system("ls -l /bin/ls");
	system("ls -l ls1");
	system("ls -l ls2");
	return 0;
}

  1. 编程实现ls的基本功能,并实现-l参数功能;如:
    1. 若没指定参数则显示当前目录的所有可见文件;
    2. 若指定参数,参数指定问文件显示该文件信息,若为目录则显示该目录中的的文件信息;
    3. -l参数格式同ls -l;
    4. 指定参数可以是多个;
      #include<stdio.h>
      #include<unistd.h>
      #include<stdlib.h>
      #include<sys/types.h>
      #include<sys/stat.h>
      #include<string.h>
      #include<time.h>
      #include<pwd.h>
      #include<grp.h>
      void filebuf(mode_t mode,char *rs){ //判断文件类型以及所有者,组和其他的权限;
      	if(S_ISDIR(mode)){
      		rs[0]='d';
      	}
      	else if(S_ISREG(mode)){
              rs[0]='-';
      	}
      	else if(S_ISFIFO(mode)){
              rs[0]='p';
      	}
      	else if(S_ISLNK(mode)){
      		rs[0]='l';
      	}
      	else if(S_ISBLK(mode)){
      		rs[0]='b';
      	}
      	else if(S_ISSOCK(mode)){
      		rs[0]='s';
      	}
      	else if(S_ISCHR(mode)){
      		rs[0]='c';
      	}
          if(mode & S_IRUSR)  rs[1]='r';
          else rs[1]='-';
          if(mode & S_IWUSR)  rs[2]='w';
          else rs[2]='-';
          if(mode & S_IXUSR)  rs[3]='x';
          else rs[3]='-';
          if(mode & S_IRGRP)  rs[4]='r';
          else rs[4]='-';
          if(mode & S_IWGRP)  rs[5]='w';
          else rs[5]='-';
          if(mode & S_IXGRP)  rs[6]='x';
          else rs[6]='-';
          if(mode & S_IROTH)  rs[7]='r';
          else rs[7]='-';
          if(mode & S_IWOTH)  rs[8]='w';
          else rs[8]='-';
          if(mode & S_IXOTH)  rs[9]='x';
          else rs[9]='-';
          rs[10]='\0';
      }
      int main(int argc, char *argv[]) {
      	struct stat fst;
      	struct tm *mytime=(struct tm *)malloc(sizeof(struct tm));
      	char rs[12];
      	if(argc!2){
              fprintf(stderr,"Usage: %s <pathname>\n",argv[0]);
              exit(EXIT_FAILURE);
      	}
      	if(stat(argv[1],&fst)==-1)
          {
              perror("stat");
              exit(EXIT_FAILURE);
          }
          filebuf(fst.st_mode,rs);
          printf("%s",rs); //输出文件类型与权限信息;
          printf(" %d",fst.st_nlink);//输出文件的硬链接数;
          printf(" %s",getpwuid(fst.st_uid)->pw_name);//输出所属用户名;
          printf(" %s",getgrgid(fst.st_gid)->gr_name);//输出用户所在组;
          printf(" %1d",fst.st_size);//输出文件大小;
          mytime=localtime(&fst.st_mtime);//获取文件修改时间;
          printf("%d-%02d-%02d%02d:%02d",mytime->tm_year+1900,mytime->
      tm_mon+1,mytime->tm_mday,mytime->tm_hour,mytime->tm_min);
          printf(" %s",argv[1]);//输出文件名;
          printf("\n");
          return 0;
      }
      
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值