windows linux—unix 跨平台通訊整合控制系統----檔案搜尋

來源:互聯網
上載者:User

標籤:

跨平台的網路通訊,跟裝置的整合控制,牽扯到在各種平台下的檔案搜尋問題,windows下面的已經有了。

地址如下:

http://blog.csdn.net/wangyaninglm/article/details/8668132

 

這篇文章主要介紹一下linux下面的檔案搜尋實現:

Filesearch.h

////  Filesearch.h//  ////  Created by mac mac on 13-4-28.//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.//#ifndef _Filesearch_h#define _Filesearch_h//#include <stdio.h>//#include <stdlib.h>#include <sys/types.h>#include <sys/stat.h>#include <dirent.h>#include <pwd.h>#include <unistd.h>//#include <string.h>#include <time.h>#include "mac.h"#include "Socket.h"//#define MAX_PATH 255int IsDir(char *name);void Search_File(char *path,char *name);//int search_flag = 0;/* int main(int argc , char *argv[]){    static char *current_dir;    static char *file_name;    int length;        if(argc==1)    {        printf("it takes more parameter!!!n");            }        if(argc==2)    {        current_dir = (char *)getcwd(current_dir,MAX_PATH);    }        if(argc==3)    {        length = strlen(argv[1]);                if(length>1 && (argv[1][length-1]=='/'))        {            argv[1][length-1]=='?';                    }        current_dir = argv[1];        file_name = argv[2];    }        Search_File(current_dir,file_name);        printf("Hello world!n");    return 0;}    */#endif


Filesearch.cpp:

////  Filesearch.cpp//  mac_client////  Created by mac mac on 13-5-21.//  Copyright (c) 2013年 __MyCompanyName__. All rights reserved.//#include <stdio.h>#include <iostream>#include "Filesearch.h"int search_flag = 0;int IsDir(char *name){    struct stat buff;    if(lstat(name,&buff)<0)        return 0;    return S_ISDIR(buff.st_mode);}//調用的時候直接使用'/'目錄作為搜尋路徑,相當於搜尋全盤了。void Search_File(char *path,char *name){    DIR *directory;    struct dirent *dir_entry;    char buffer[MAX_PATH];    if((directory = opendir(path)) == NULL)    {        fprintf(stderr,"%s",path);        printf(path);        perror(" ");        return;    }    while(dir_entry == readdir(directory))    {        if(!strcmp(dir_entry->d_name,".")||!strcmp(dir_entry->d_name,".."))        {            //do nothing        }        else        {            if((strcmp(path,"/")) == 0)            {                sprintf(buffer,"%s%s",path,dir_entry->d_name);                // printf(buffer);                /*  if is not  boot  directory do not add "/"*/            }            else            {                sprintf(buffer,"%s/%s",path,dir_entry->d_name);                printf(buffer);                printf("\n");            }            if(IsDir(buffer))            {                Search_File(buffer,name);            }            else            {                //find the file,if exist                if(strcmp(dir_entry->d_name,name)==0)                {                    printf("%sn",buffer);                    search_flag=1;                }            }        }    }    closedir(directory);}void setOutFiles(const char * path)//得到指定目錄下面所有檔案, 傳輸的時候還得改{    DIR *dp;    struct dirent *dirp;    char fullpath[MAX_PATH] = {0};    if((dp = opendir(path)) == NULL)    {        //err_quit();        return ;    }    if (strcmp(path,"/") == 0) //如果是根目錄,要處理一下    {        while((dirp = readdir(dp))!= NULL)        {            sprintf(fullpath,"%s%s", path,dirp->d_name);            printf("%s\n",fullpath);        }    }    else    {        while((dirp = readdir(dp))!= NULL)        {            sprintf(fullpath,"%s/%s", path,dirp->d_name);            printf("%s\n",fullpath);        }    }}


 

 

windows linux—unix 跨平台通訊整合控制系統----檔案搜尋

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.