使用open,read,write,close实现简单的touch,cat,命令,了seek函数代码举例

实现一个cat命令

  1 #include<stdio.h>
  2 #include<sys/stat.h>
  3 #include<unistd.h>
  4 #include<sys/types.h>
  5 #include<fcntl.h>
  6 int main (int argc,char *argv[])
  7 {
  8     if (argc !=2)       //指定输入格式,两个变量 
  9     {
 10         printf("./a.out filename\n"); 
 11         return -1;
 12     }
 13     int fd = open(argv[1],O_RDONLY);    //以可读形式打开文件
 14     char buf[256];              //设置缓冲区
 15     while(1)        //缓冲区有限循环读取
 16     {
 17         int  ret = read(fd,buf,sizeof(buf));  //读取文件内容
 18         write(STDOUT_FILENO,buf,ret);     //输入到终端,第一个参数代表fd中的1标准输出
 19         if (ret ==0)
 20         {
 21             break;
 22         }
 23     }
 24     close (fd);
 25     return 0;                                                                              
 26 }

实现一个touch简易版:

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<sys/stat.h>                                                                       
  4 #include<fcntl.h>
  5 #include<unistd.h>
  6 int main (int argc,char *argv[])
  7 {
  8     if (argc !=2)
  9     {
 10         printf ("./a.out filename\n");
 11     }
 12     int fd=open(argv[1],O_RDONLY|O_CREAT,0666);
 13     close(fd);
 14     return 0;
 15 }

lseek计算文件大小:

  1 #include<stdio.h>                                                                          
  2 #include<sys/types.h>
  3 #include<unistd.h>
  4 #include<sys/stat.h>
  5 #include<fcntl.h>
  6 
  7 int main (int argc,char * argv[])
  8 {
  9     if (argc != 2)
 10     {
 11         printf ("./filename\n");
 12         return -1;
 13     }
 14     //open一个文件  
 15     int fd  = open(argv[1],O_RDONLY);
 16     //调整文件读写位置,返回值为文件开头到文件末尾的长度,利用返回值进行查看文件大小
 17     int ret = lseek(fd,0,SEEK_END);
 18     printf("this file size is %d\n",ret);
 19     //关闭文件描述符
 20     close(fd);
 21     return 0;
 22 }

lseek拓展文件:

  1 #include<stdio.h>
  2 #include<sys/types.h>
  3 #include<unistd.h>
  4 #include<sys/stat.h>
  5 #include<fcntl.h>
  6 
  7 int main (int argc,char * argv[])
  8 {
  9     if (argc != 2)
 10     {
 11         printf ("./filename\n");
 12         return -1;
 13     }
 14     //open一个文件,以写方式打开要是不存在则创建
 15     int fd  = open(argv[1],O_WRONLY|O_CREAT,0666);
 16     //lseek拓展文件,设置偏移量也就是拓展大小
 17     int ret = lseek(fd,1024,SEEK_END);
 18     //lseek拓展特性必须先写入一次不然不会生效
 19     write(fd,"a",1);
 20     //关闭文件描述符
 21     close(fd);
 22     return 0;
 23 }         

sleek调整读写位置:

  1 #include<stdio.h>                                                                          
  2 #include<unistd.h>
  3 #include<sys/stat.h>
  4 #include<sys/types.h>
  5 #include<fcntl.h>
  6 int main(int argc,char*argv[])
  7 {
  8     if (argc!=2)
  9     {
 10         char buf[20]="./filename\n";
 11         write(STDOUT_FILENO,buf,sizeof(buf));
 12         return -1;
 13     }
 14     int fd = open(argv[1],O_RDWR|O_CREAT,0666);
 15     //打开一个文件,若这个文件不存在则创建,创建出来的文件权限为666&~权限掩码umask
 16     //用一个文件描述符指向这个文件
 17     write(fd,"helloworld\n",12);
 18     //write函数写入helloword,在这个文件描述符指向,大小12个字节
 19     lseek(fd,0,SEEK_SET);
 20     //由于写入了helloworld,所以这个文件的的读写位置指到了文件末尾
 21     //要是这个时候直接进行读取的话时读不到内容的,因为读写位置已经指到了末尾
 22     char buf1[256];
 23     int ret = read(fd,buf1,sizeof(buf1));
 24     //文件读写位置调整之后,再对文件进行读操作,读取长度为sizeof(buf1),内容存到缓冲区
 25     //返回值为读到的长度ret
 26     if (ret)
 27     {
 28         write(STDOUT_FILENO,buf1,ret);
 29     }
 30     //如果读到内容则就进行输出
 31     close(fd);
 32     return 0;
 33 }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值