Chinaunix首页 | 论坛 | 博客
  • 博客访问: 893614
  • 博文数量: 299
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 2493
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-21 10:07
个人简介

Linux后台服务器编程。

文章分类

全部博文(299)

文章存档

2015年(2)

2014年(297)

分类: LINUX

2014-07-07 22:38:07

之前有介绍过关于文件的指针和描述符,这次通过一个练习,熟悉了一下文件的open,close,read,write,sleek,dup等操作,一些主要的注意事项详见代码注释吧。
 
ps:部分代码写的有些龌龊,也和硬要把几个函数都试到有关,应该可以用更好的方法。fighting~~~
 
【功能】命令行输入三个参数,将data.dat文件中的内容拷贝到data2.dat中,并搜索data2.dat中hello出现的次数,消息打印重定向到dupfile.dat中。
 
【代码实现】
#include
#include
#include
#include
#include
#include
int main(int argc,char* argv[])
{
 int fd1;
 int fd2;
 int fd3;
 char buffer[100]; 
 int num;
 int flag = 0;
 int len;
 int offset = 0;
 if(4 != argc)
 {
  printf("Usage:%s source file,dest file,key word.\n",argv[0]);
  return 1;
 }

 // test function :open(),close(),read(),write()
 //打开文件,如果文件不存在,允许按照参数给定权限创建,这里文件data.dat为已经存在且写入一定内容的。
 if((fd1=open(argv[1],O_CREAT | O_RDWR,0777))==-1)  
 {
  perror("Can't open the source file.\n");
  return 1;
 }
 
 if((fd2=open(argv[2],O_CREAT | O_RDWR,0777))==-1)
 {
  perror("Can't open the dest file.\n");
  return 1;
 }
 while((num=read(fd1,buffer,5))>0)   
 {
  buffer[num]='\0';    //防止读出乱码影响结果
  if(write(fd2,buffer,num)==-1)
  {
   perror("Write to file data2.dat failed.\n");
   return 1;
  } 
 }
 close(fd1);
 close(fd2);
 
 //test function: lseek()  dup()
 len = strlen(argv[3]);
 if((fd2=open(argv[2],O_RDONLY))==-1)
 {
  perror("Can't open the dest file.\n");
  return 1;
 }
 while(1)
 {
//SEEK_SET参数表示每次都是直接用offset做偏移值,即在文件头的位置+offset;此外SEEK_CUR表示在当前位置基础上加offset偏移,SEEK_END表示偏移量为文件大小加offset值。
  if(lseek(fd2,offset,SEEK_SET)==-1)
  {
   perror("Can't move the file pointer.\n");
   return 1;
  }
  if((num=read(fd2,buffer,len))   {
//可读到的字符数小于要搜索的字符串长度,可以跳出
   break;
  }
  else 
  { 
   buffer[len]='\0';
   if(strcmp(buffer,argv[3])==0)
   {
    //找到匹配,计数+1
    flag++;
   }
//无论是否匹配,offset都应该增加1,否则文件指针一直不变,进入死循环
   offset++;
  }
 }
//关闭标准输出,并将打开的文件描述符重定向到标准输出
 close(STDOUT_FILENO);
 if((fd3=open("dupfile.dat",O_CREAT | O_RDWR,0777))==-1)
 {
  perror("Can't creat the dup file.\n");
  return 1;
 }
 if(dup(fd3)==-1)
 {
  perror("Can't reserved the std out fd.\n");
  return 1;
 }
 if(flag>0)
 {
//printf打印的内容实际上已经重定向到dupfile.dat中了
  printf("Find the string %s in file %s %d times.\n",argv[3],argv[2],flag);
 }
 close(fd2);
 return 0;
}
【运行结果】

 gcc -o file file.c


 ./file data.dat data2.dat hello   //带3个参数执行

 cat data.dat
hellohellohellohellohellohellohellohe
 cat data2.dat   //从data.dat中拷贝的内容
hellohellohellohellohellohellohellohe
 cat dupfile.dat   //打印hello出现7次重定向到dupfile.dat
Find the string hello in file data2.dat 7 times.
阅读(1371) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~