这是查找该目录下指定文件类型的用 vc 6.0 编写 的关于文件搜索的例子,
不过搜索子文件夹还有问题
#include <stdio.h>
#include <windows.h>
#include <io.h>
void fileset(char* path, char* name)
{
FILE* fp_int;
FILE* fp_out1;
FILE* fp_out2;
char allname[1024];
char *FileBuff = NULL;
int filelen = 0, len = 0, flag = 0;
char *tmp = NULL;
char *ptr = NULL;
char *subname = NULL;
sprintf(allname, "%s//%s", path, name);
fp_int = fopen(allname, "rb");
if (fp_int != NULL)
{
fseek(fp_int, 0, SEEK_END);
filelen = ftell(fp_int);
rewind(fp_int);
FileBuff = (char*)malloc(filelen+2);
memset(FileBuff, 0, filelen+2);
fread(FileBuff, filelen, 1, fp_int);
fclose(fp_int);
len = strlen(name);
tmp = strstr(name, ".");
subname = name;
subname[len-4] = '/0';
sprintf(allname, "%s//%s__1.txt", path, name);
fp_out1 = fopen(allname, "wb");
sprintf(allname, "%s//%s__2.txt", path, name);
fp_out2 = fopen(allname, "wb");
ptr = FileBuff;
while(*ptr != '/0')
{
if (*ptr == 0x0d)
{
flag ++;
}
if (flag % 2 == 0)
{
fwrite(ptr, 1, 1, fp_out1);
}
else
{
fwrite(ptr, 1, 1, fp_out2);
}
if (flag >= 10000)
{
flag = 0;
}
ptr ++;
}
free(FileBuff);
fclose(fp_out1);
fclose(fp_out2);
}
}
void searchdefaultfile(char* path)
{
struct _finddata_t c_file_RALF;
int findNewFile = 0;
int hFile;
char allname[1024];
sprintf(allname, "%s//*.txt", path);
c_file_RALF.attrib = _A_NORMAL | _A_RDONLY | _A_ARCH;
hFile = _findfirst(allname, &c_file_RALF );
if(hFile != -1L)
{
findNewFile = 1;
fileset(path, c_file_RALF.name);
}
else
{
findNewFile = 0;
}
while( findNewFile )
{
if( _findnext( hFile, &c_file_RALF ) == 0)
{
findNewFile = 1;
fileset(path, c_file_RALF.name);
}
else
{
findNewFile = 0;
}
}
_findclose(hFile);
}
void searchdefaultdir(char* path)
{
struct _finddata_t c_file_RALF;
int findNewFile = 0;
int hFile;
char allname[1024];
searchdefaultfile(path);//找文件
//取目录
c_file_RALF.attrib = _A_SUBDIR;
sprintf(allname, "%s//*", path);
hFile = _findfirst(allname, &c_file_RALF );
if(hFile != -1L)
{
findNewFile = 1;
sprintf(allname, "%s//%s", path, c_file_RALF.name);
searchdefaultdir(allname);
}
else
{
findNewFile = 0;
}
while( findNewFile )
{
if( _findnext( hFile, &c_file_RALF ) == 0)
{
findNewFile = 1;
sprintf(allname, "%s//%s", path, c_file_RALF.name);
searchdefaultdir(allname);
}
else
{
findNewFile = 0;
}
}
_findclose(hFile);
}
void main()
{
char path[1024];
int FindHandle = 0;
struct _finddata_t FindData;
GetCurrentDirectory(1023, path);
searchdefaultdir(path);
}
本文提供了一个使用VC6.0进行文件搜索的示例代码,主要针对.txt文件进行处理,并尝试递归地搜索子目录。通过该示例可以了解如何在Windows环境下利用C语言实现基本的文件搜索功能。
1181

被折叠的 条评论
为什么被折叠?



