查找该目录下指定文件类型的用 vc 6.0 编写 的关于文件搜索的例子
2010-03-10 17:49
1091 查看
这是查找该目录下指定文件类型的用 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);
}
不过搜索子文件夹还有问题
#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);
}
相关文章推荐
- C++及API函数实现查找某目录下指定类型的文件
- 编写一个Python程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径
- Python:编写函数,能在当前目录以及子目录下查找文件名包含指定字符串的文件,并打印出完整路径
- php下实现在指定目录搜索指定类型文件的函数
- php下实现在指定目录搜索指定类型文件的函数
- 编写一个程序,能在当前目录以及当前目录的所有子目录下查找文件名包含指定字符串的文件,并打印出相对路径。
- 用grep查找指定目录及其子目录下的特定文件类型
- MFC 目录选取,指定类型文件查找复制
- grep如何递归目录并在指定类型文件中查找
- vb 搜索指定目录下的指定类型文件
- 在当前目录中所有文件中,查找,存在指定内容的指定文件类型
- c++查找目录下的指定类型的全部文件
- grep命令在指定目录中的指定文件类型中查找关键字
- win32控制台下实现查找并显示指定目录下的指定类型文件
- 002_018 Python 查找目录中的第一个文件,指定类型
- 在指定目录中查找指定类型的文件,并且获取文件名再显示
- [Shell]在一个目录及其子目录内的指定类型的文件中搜索某个字符串
- linux 指定文件类型 全目录查找
- UE4 C++实现搜索指定目录下的指定类型的所有文件
- vc__在指定目录下查找文件