windows linux―unix 跨平台通信集成控制系统—-文件搜索

跨平台的网络通讯,跟装备的集成控制,牵扯到在各种平台下的文件搜索问题,windows下面的已有了。

地址以下:

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

 

这篇文章主要介绍1下linux下面的文件搜索实现:

Filesearch.h

//
// Filesearch.h
// //
// Created by mac mac on 13⑷⑵8.
// 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 255

int 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⑴]=='/'))
{
argv[1][length⑴]=='?';

}
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⑸⑵1.
// 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("
");
}

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) //如果是根目录,要处理1下
{
while((dirp = readdir(dp))!= NULL)
{

sprintf(fullpath,"%s%s", path,dirp->d_name);
printf("%s
",fullpath);

}

}
else
{

while((dirp = readdir(dp))!= NULL)
{

sprintf(fullpath,"%s/%s", path,dirp->d_name);
printf("%s
",fullpath);

}
}

}

 

 

波比源码 – 精品源码模版分享 | www.bobi11.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,您必须在下载后24小时内删除!
3. 不得使用于非法商业用途,不得违反国家法律。否则后果自负!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 本站源码并不保证全部能正常使用,仅供有技术基础的人学习研究,请谨慎下载
8. 如遇到加密压缩包,请使用WINRAR解压,如遇到无法解压的请联系管理员!

波比源码 » windows linux―unix 跨平台通信集成控制系统—-文件搜索

1 评论

  1. Thank you for content. Area rugs and online home decor store. Hello Administ .

发表评论

Hi, 如果你对这款模板有疑问,可以跟我联系哦!

联系站长
赞助VIP 享更多特权,建议使用 QQ 登录
喜欢我嘛?喜欢就按“ctrl+D”收藏我吧!♡