try_dir.c

来自「将分词后的文档内容按照word的内容」· C语言 代码 · 共 107 行

C
107
字号
#include "stdio.h"
#include "stdlib.h"
#include "dir.h"
#include "string.h"
#include "dos.h"

/*This function is for dir browse. And it is based on turbo C.

The first parameter is the direct which is to be browsed. For example: "c:\turboc\11\".

The second parameter is the special file require. For example: "*.txt".

This function browse all files in this dir, including subdir.

The last but six line is make dir in the current dir.

If want to get current dir, you can use the getcurdir(0,buf) function.

*/

int BrowseDir(const char *dir,const char *filespec)
{
	struct ffblk *fileinfo;
	char filename[MAXPATH];
	int count = 0;
	int dircount = 0;
	char *buf;
	char *subdir;
	buf = (char *) malloc(40);
	strcpy(buf, dir);
/*	printf("%s\n",buf);*/

	/*chdir(buf);*/
	/*printf("%s\n",buf);*/
	fileinfo = (struct ffblk *)malloc(sizeof(struct ffblk));
	strcat(buf, filespec);
/*  	printf("%s\n",buf);*/
	if ((findfirst(buf, fileinfo, FA_ARCH)) == 0)
	{
		do
		{
/*			printf("fileinfo.ff_attrib is : %s\n", fileinfo->ff_attrib); */
/*			printf("fileinfo.ff_name is : %s\n", fileinfo->ff_name);     */
			if ((fileinfo->ff_attrib & FA_ARCH))     /*   if it is file  */
			{
				count++;
				printf("fileinfo.ff_attrib is : %s\n", fileinfo->ff_attrib);
				printf("fileinfo.ff_name is : %s\n", fileinfo->ff_name);
                /************  file open code add here!     ************/
				
			}
		} while (findnext(fileinfo) == 0);
	}
	printf("number of txt is: %d\n", count);

	count = 0;
	dircount = 0;
	free(fileinfo);
	fileinfo = (struct ffblk *)malloc(sizeof(struct ffblk));
	free(buf);
	buf = (char *)malloc(40);

	strcpy(buf,dir);
	strcat(buf,"*.*");

	if ((findfirst(buf, fileinfo, FA_DIREC)) == 0)
	{
		do
		{
			if ((fileinfo->ff_attrib & FA_DIREC))   /*  if it is subdir  */
			{
				if((strcmp(fileinfo->ff_name, ".") != 0)&&(strcmp(fileinfo->ff_name, "..") != 0))
      			/* if the name of subdir is not "." or "..", because this is the hidden file */
				{
					printf("fileinfo.ff_attrib is : %s\n", fileinfo->ff_attrib);
					printf("fileinfo.ff_name is : %s\n", fileinfo->ff_name);
					dircount++;
					subdir = (char *) malloc( MAXPATH * sizeof(char));
					strcpy(subdir, dir);
					strcat(subdir, fileinfo->ff_name);
					strcat(subdir, "\\");
					BrowseDir(subdir,filespec);
				}
			}
		} while (findnext(fileinfo) == 0);
	}
	printf("number of dir in %s is: %d\n", dir, dircount);
}

main()
{
	char *buf;
	buf = (char *)malloc(40);
    strcpy(buf,"c:\\turboc2\\11\\");
	/*getcurdir(0,buf);
	printf("%s\n",buf);*/

	BrowseDir(buf,"*.txt");
/*	strcat(buf,"\\data");
	if(mkdir(buf) != 0)
	{
		printf("mkdir is error!\n");
	}
*/
	getch();
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?