win+Linux下查找指定后缀名文件(在当前目录查找)

1、第一种方式

参考网络上的代码加以修改,已经在linux上测试通过,可以找到当前路径下指定后缀名的文件,有多少个符合的就找到多少个。其中有一个函数比较关键:rindex,具体用法参见百度百科,一目了然:https://baike.baidu.com/item/rindex/3899189?fr=aladdin

#include "stdio.h"
#include "stdlib.h"
#include "malloc.h"
#include "string.h"
#include "dirent.h"

int main(void)
{	
	DIR *dir;
    struct dirent *ptr;
	char *DirPath="./";
	char *FileExtName=".log";
	char *CurFileExtName = NULL;
 
    if ((dir=opendir(DirPath)) == NULL)
    {
        perror("Open dir error...");
        exit(1);
    }
	
	  while ((ptr=readdir(dir)) != NULL)
    { 
//char *rindex(const char *s, int c);rindex()用来找出参数s 字符串中最后一个出现的参数c 地址,
//然后将该字符出现的地址返回。字符串结束字符(NULL)也视为字符串一部分。
        CurFileExtName = rindex(ptr->d_name, '.');
        if(CurFileExtName!=NULL&& strcmp(CurFileExtName,FileExtName) == 0)
        {              
		   printf("CurFilePath=%s %s\n",DirPath,ptr->d_name);              
		}         
    }
    closedir(dir);
	return 0;

}

2、第二种方式


#ifdef WINDOWS
#include <process.h>
#include <windows.h>
#include <io.h>
#include <conio.h>

#else
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <termios.h>
#include<dirent.h>
#endif


/*********************************************文件查找*******************************************/
#ifdef WINDOWS
	long handle;
	struct _finddata_t ff;   
	char *dir = "";                       
	char *file = "*.txt";					
	char select[256];			          
	sprintf(select, "%s%s", dir, file);		 
	handle = _findfirst(select, &ff);
	if (-1 == handle)
	{
		printf("The script file not find !\n");
		printf("\nPress any key to exit...\n"); 
		GETCH();
		return -1;
	}
	else
	{
		printf("Find the script file is:%s\n", ff.name); 
		strcpy(filename, ff.name);
	}		
	
	_findclose(handle); 

#else  //Linux
	DIR *dir;
	struct dirent *ptr;
	char *dirpath = "./";
	char *filenames = ".txt";
	char *curfilename = NULL;
	int findflag = 0;
	if ((dir = opendir(dirpath)) == NULL)
	{
		perror("Open dir error...");
		exit(1);
    }
	while ((ptr = readdir(dir)) != NULL)//读取到目录文件尾则返回NULL
	{
		curfilename = rindex(ptr->d_name, '.');
		if (curfilename != NULL&& strcmp(curfilename, filenames) == 0)
		{
			//printf("find file name :%s\n", ptr->d_name);   //ptr->d_name为找到的文件名
			findflag = 1;
			break;
		}
	}
	if (0 == findflag)
	{
		printf("The script file not find !\n");
		printf("\nPress any key to exit...\n");
		GETCH();
		return -1;
	}
	else
	{	
		strcpy(filename, ptr->d_name);
		printf("Find the script file is: %s\n", filename);
	}
	closedir(dir);

#endif
/*************************************************************************************************/

3、封装方式

.h文件
#pragma once

#ifdef _WIN32

#include <io.h>
#include <windows.h>
#pragma comment(lib, "wsock32.lib")


#else
#include <arpa/inet.h>
#include <dirent.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/time.h>
#include <termios.h>
#include <dirent.h>


int getch_unix(void)//
{
	struct termios oldt, newt;
	int ch;

	tcgetattr(0, &oldt);

	newt = oldt;

	newt.c_lflag &= ~(ICANON | ECHO);

	tcsetattr(0, TCSANOW, &newt);

	ch = getchar();

	tcsetattr(0, TCSANOW, &oldt);

	return ch;
}

#endif // _WIN32

#define NSOP_OK       0

#ifdef  _WIN32
#define GETCH() getchar()
#define SLEEP(msec) Sleep(msec)
#define	THREAD_EXIT() _endthread() 
#else
#define GETCH() getch_unix()
#define SLEEP(msec) usleep(msec*1000)
#define	THREAD_EXIT() pthread_exit(NULL)
#endif



/*********************************************文件查找*******************************************/

//参数1:文件后缀名,如"*.cert"   参数2:找到的符合条件的文件名
int Find_File(char* filesuf ,char filename[256])
{

#ifdef _WIN32
	long handle;
	struct _finddata_t ff;
	char *dir = "";
	
	//char *filesuf = "*.cert";
	
	char select[256];
	sprintf(select, "%s%s", dir, filesuf);
	handle = _findfirst(select, &ff);
	if (-1 == handle)
	{
		printf("The  file not find !\n");
		printf("\nPress any key to exit...\n");
		GETCH();
		return -1;
	}
	else
	{
		printf("Find the  file is:%s\n", ff.name);
		strcpy(filename, ff.name);
	}

	_findclose(handle);

#else  //Linux
	DIR *dir;
	struct dirent *ptr;
	char *dirpath = "./";
	
	//为了与windows平台统一,去掉了后缀名的通配符*,linux不需要通配符,只需要后缀名
	char filenames[64] = { 0 };
	memcpy(filenames, &filesuf[1], strlen(filesuf) - 1);
	//char *filesuf = ".cert";
	
	char *curfilename = NULL;
	int findflag = 0;
	if ((dir = opendir(dirpath)) == NULL)
	{
		perror("Open dir error...");
		exit(1);
	}
	while ((ptr = readdir(dir)) != NULL)//读取到目录文件尾则返回NULL
	{
		curfilename = rindex(ptr->d_name, '.');
		if (curfilename != NULL&& strcmp(curfilename, filenames) == 0)
		{
			//printf("find file name :%s\n", ptr->d_name);   //ptr->d_name为找到的文件名
			findflag = 1;
			break;
		}
	}
	if (0 == findflag)
	{
		printf("The script file not find !\n");
		printf("\nPress any key to exit...\n");
		GETCH();
		return -1;
	}
	else
	{
		strcpy(filename, ptr->d_name);
		printf("Find the script file is: %s\n", filename);
	}
	closedir(dir);

#endif

	return 0;

}
/*************************************************************************************************/

.c文件
   
   void main()
   {
    char certname[256] = {0};
	char* filesuf = "*.cert";
	if (0 != Find_File(filesuf, certname))//查找符合后缀名的文件
	{
		return OPT_RETURN;
	}
	
	FILE *file;
	file = fopen(certname, "rb+");//打开找到的这个文件
	if (file == NULL)
	{
		perror("fopen");
		return -1;
	}

	fseek(file, 0, SEEK_END);
	int len = ftell(file);
	printf("\nfile len = %d\n", len);//判断文件长度
	fseek(file, 0, SEEK_SET);

    //开辟空间存储文件数据
	unsigned char *certdata = NULL;
	certdata = (unsigned char *)malloc(len * sizeof(unsigned char));
	memset(certdata, 0, len);


	//从文件中读取证书数据
	int aclen = fread(certdata, sizeof(unsigned char), len, file);
	if (aclen != len)
	{
		printf("fread data len = %d\n", aclen);
		return OPT_RETURN;
	}

	//打印证书的数据
	printf("\ncert data:\n");
	for (int i = 0; i < 143; i++)
	{
		printf("0x%02x,", certdata[i]);
		if (0 == (i + 1) % 16)
			printf("\n");
	}
	printf("\n");
}


版权声明:本文为weixin_44618297原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。