文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>linux文件I/O操作

linux文件I/O操作

时间:2009-04-11  来源:sjhf

之前有介绍过关于文件的指针和描述符,这次通过一个练习,熟悉了一下文件的open,close,read,write,sleek,dup等操作,一些主要的注意事项详见代码注释吧。   ps:部分代码写的有些龌龊,也和硬要把几个函数都试到有关,应该可以用更好的方法。fighting~~~   【功能】命令行输入三个参数,将data.dat文件中的内容拷贝到data2.dat中,并搜索data2.dat中hello出现的次数,消息打印重定向到dupfile.dat中。   【代码实现】 #include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
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))<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;
}
【运行结果】 gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ gcc -o file file.c
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ ./file data.dat data2.dat hello   //带3个参数执行
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$
gaolu@gaolu-desktop:~$ cat data.dat
hellohellohellohellohellohellohellohe
gaolu@gaolu-desktop:~$ cat data2.dat   //从data.dat中拷贝的内容
hellohellohellohellohellohellohellohe
gaolu@gaolu-desktop:~$ cat dupfile.dat   //打印hello出现7次重定向到dupfile.dat
Find the string hello in file data2.dat 7 times.
gaolu@gaolu-desktop:~$
   

http://keren.blog.51cto.com/720558/144901

相关阅读 更多 +
排行榜 更多 +
剑术大乱斗

剑术大乱斗

动作格斗 下载
南钛HSSE

南钛HSSE

商务办公 下载
柱子

柱子

动作格斗 下载