Example #1
0
void
test_readv()
{
    char                    buf0[2];
    char                    buf1[3];
    char                    buf2[4];
    struct iovec            my_iovec[3];
    ssize_t                 bytes_read;
    char                    *ptmpfile = "tmp123.txt";
    int                     fd;

    my_iovec[0].iov_base = buf0;
    my_iovec[0].iov_len= sizeof(buf0);
    my_iovec[1].iov_base = buf1;
    my_iovec[1].iov_len = sizeof(buf1);
    my_iovec[2].iov_base = buf2;
    my_iovec[2].iov_len = sizeof(buf2);

    if((fd = create_file(ptmpfile)) < 0)
        return;
    
    printf("begin test readv:\n");
    bytes_read = readv(fd, my_iovec, 3);/*这儿还有点问题,不晓得为什么总返回0*/
    printf("\n\nbytes_read[%d]\n", bytes_read);
    if(bytes_read < 0){
        perror("readv");
    }

    dele_file(ptmpfile);
    
}
Example #2
0
///递归遍历指定文件夹,并输出所有文件
void file_list(const char*fileName,int layer){
   DIR *pRec = opendir(fileName);
   if(pRec){
      struct dirent *pDir;
      while( (pDir = readdir(pRec)) ){
         char path[PATH_MAX+1];
         memset(path,'\0',PATH_MAX+1);
         int len = strlen(fileName);//without '\0'
         strncpy(path,fileName,len);
         strcat(path,"/");
         strcat(path,pDir->d_name);
         //1 表示是目录,0表示是文件
         int type = get_file_type(path);
         if(type){
            //当不是.和..时才进入下一级目录
            if(strcmp(pDir->d_name,".") && strcmp(pDir->d_name,"..")){
               //show_file(pDir->d_name,layer);
               file_list(path,++layer);
              }else{
                //printf("test:%s\n",pDir->d_name);
                //show_file(pDir->d_name,layer);
             }
         }else{
             //show_file(pDir->d_name,layer);
             //直接删除文件
             dele_file(path);
        }
      }
      //此时文件夹中的文件已经全部被删除
      dele_dir(fileName);  
   }else{
       //show_file(fileName,--layer);
       // Since file_suc is not equal to zero that means the specify path 
       // is legal.
       if(!file_suc){
          printf("Specify path is illegal,please check again.\n");
       }
   }
}