遍历文件的算法,有没有大神可以优化一下的

xy505188 2014-05-08 06:51:48
自己写的一个遍历文件夹及子文件夹下所有文件 的代码,不想使用全局变量,我的想法是写成:
vector<ff> filesearch(char newpath[_MAX_PATH])
{
vector<ff> v;
///···///
return v;
}
的形式,用函数返回值的形式来取得遍历后的内容
全局变量如果函数多次调用后,不知道值已经变成什么了,新人水平不高,不知道该如何才能优化,主要就是
filesearch()如果重复调用自己的话,自己所声明的变量就会被覆盖(说法可能不准确)
如何才能vector<ff> v不是全局变量,同时又不被函数调用自己时覆盖?
#include <vector>
#include <algorithm>
#include <stdio.h>
#include <io.h>
#include <windows.h>
#include <stdlib.h>
using namespace std;

typedef struct ff
{
char p[_MAX_PATH]; ///完整路径
char n[_MAX_FNAME]; ///文件名
};

vector<ff> v;

void filesearch(char newpath[_MAX_PATH])
{
struct _finddata_t fd;
int handle,done=0;
char path[_MAX_PATH];
strcpy(path,newpath);
strcat(path,"\\*.*");
if(!(handle=_findfirst(path,&fd)))
{
return ;
}
while(!(done=_findnext(handle,&fd)))
{
if(strcmp(fd.name,".")==0 || strcmp(fd.name,"..")==0)
{
continue;
}
if(fd.attrib==_A_SUBDIR)
{
char t[_MAX_PATH];
strcpy(t,newpath);
strcat(t,"\\");
strcat(t,fd.name);
filesearch(t);
continue;
}
else
{
ff f;
char t[_MAX_PATH];
strcpy(t,newpath);
strcat(t,"\\");
strcat(t,fd.name);
strcpy(f.p,t);
strcpy(f.n,fd.name);
v.push_back(f);
}
}
_findclose(handle);
}

int main()
{
FILE *in;
in=fopen("1.txt","w");
filesearch("f:\\11");
for(vector<ff>::size_type i=0;i<v.size();i++)
{
fprintf(in,"%s\n",v[i].p);
}
fclose(in);
system("start 1.txt");
return 0;
}
...全文
95 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2014-05-09
  • 打赏
  • 举报
回复
推荐楼主阅读《Unix编程艺术》
赵4老师 2014-05-09
  • 打赏
  • 举报
回复
system("dir /b /a-d c:\\*.* >d:\\allfiles.txt"); //读文件d:\\allfiles.txt的内容即C:\\下所有文件的名字 system("dir /b /a-d /s c:\\*.* >d:\\allfilesinsub.txt"); //读文件d:\\allfilesinsub.txt的内容即C:\\下所有文件的名字包含子目录 system("dir /b /ad c:\\*.* >d:\\alldirs.txt"); //读文件d:\\alldirs.txt的内容即C:\\下所有子目录的名字 请记住,能用shell命令获取文件、文件夹信息或者操作文件、文件夹最好用shell命令获取或者操作,而不要用各种API获取或者操作,因为当遇到非法文件夹名或非法文件名或非法文件长度、非法文件日期、压缩文件、链接文件、稀疏文件……等各种意料之外的情况时,API会处理的不全面或陷入死循环,而shell命令不会。 如果嫌system弹出cmd窗口难看的话,改用WinExec("...",SW_HIDE);
walker沃克 2014-05-08
  • 打赏
  • 举报
回复
引用 5 楼 xy505188 的回复:
[quote=引用 2 楼 anonymalias 的回复:]

void  filesearch(char newpath[_MAX_PATH],   vector<ff> &v)
{
}
多谢大神指点[/quote] 这个问题不能说是大神,,,大神是不可以乱叫的
xy505188 2014-05-08
  • 打赏
  • 举报
回复
引用 2 楼 anonymalias 的回复:

void  filesearch(char newpath[_MAX_PATH],   vector<ff> &v)
{
}
多谢大神指点
xy505188 2014-05-08
  • 打赏
  • 举报
回复
引用 3 楼 hemmingway 的回复:


#include <vector>
#include <algorithm>
#include <stdio.h>
#include <io.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct
{
	char p[_MAX_PATH];  ///完整路径
	char n[_MAX_FNAME];  ///文件名
}ff;


void filesearch(char newpath[_MAX_PATH]/*in*/, vector<ff>& fvec )
{
	struct _finddata_t fd;
	int handle, done = 0;
	char path[_MAX_PATH];
	strcpy_s(path,_MAX_PATH, newpath);
	strcat_s(path,_MAX_PATH, "\\*.*");
	if (!(handle = _findfirst(path, &fd)))
	{
		fvec.clear();
		return;
	}

	while (!(done = _findnext(handle, &fd)))
	{
		if (strcmp(fd.name, ".") == 0 || strcmp(fd.name, "..") == 0)
		{
			continue;
		}
		if (fd.attrib == _A_SUBDIR)
		{
			char t[_MAX_PATH];
			strcpy_s(t,_MAX_PATH, newpath);
			strcat_s(t, _MAX_PATH,  "\\");
			strcat_s(t, _MAX_PATH, fd.name);
			filesearch(t, fvec);
			continue;
		}
		else
		{
			ff f;
			char t[_MAX_PATH];
			strcpy_s(t, newpath);
			strcat_s(t, "\\");
			strcat_s(t, fd.name);
			strcpy_s(f.p, t);
			strcpy_s(f.n, fd.name);
			fvec.push_back(f);
		}
	}
	_findclose(handle);
}



int main()
{
	vector<ff> fvec;
	FILE *in;

	fopen_s(&in, "1.txt", "w");
	filesearch("f:\\my_proj", fvec);
	for (vector<ff>::size_type i = 0; i < fvec.size(); i++)
	{
		fprintf(in, "%s\n", fvec[i].p);
	}
	fclose(in);
	system("start 1.txt");

}
谢谢大神点拨,同时更深入理解了vector的原理
hemmingway 2014-05-08
  • 打赏
  • 举报
回复


#include <vector>
#include <algorithm>
#include <stdio.h>
#include <io.h>
#include <iostream>
#include <stdlib.h>
using namespace std;

typedef struct
{
	char p[_MAX_PATH];  ///完整路径
	char n[_MAX_FNAME];  ///文件名
}ff;


void filesearch(char newpath[_MAX_PATH]/*in*/, vector<ff>& fvec )
{
	struct _finddata_t fd;
	int handle, done = 0;
	char path[_MAX_PATH];
	strcpy_s(path,_MAX_PATH, newpath);
	strcat_s(path,_MAX_PATH, "\\*.*");
	if (!(handle = _findfirst(path, &fd)))
	{
		fvec.clear();
		return;
	}

	while (!(done = _findnext(handle, &fd)))
	{
		if (strcmp(fd.name, ".") == 0 || strcmp(fd.name, "..") == 0)
		{
			continue;
		}
		if (fd.attrib == _A_SUBDIR)
		{
			char t[_MAX_PATH];
			strcpy_s(t,_MAX_PATH, newpath);
			strcat_s(t, _MAX_PATH,  "\\");
			strcat_s(t, _MAX_PATH, fd.name);
			filesearch(t, fvec);
			continue;
		}
		else
		{
			ff f;
			char t[_MAX_PATH];
			strcpy_s(t, newpath);
			strcat_s(t, "\\");
			strcat_s(t, fd.name);
			strcpy_s(f.p, t);
			strcpy_s(f.n, fd.name);
			fvec.push_back(f);
		}
	}
	_findclose(handle);
}



int main()
{
	vector<ff> fvec;
	FILE *in;

	fopen_s(&in, "1.txt", "w");
	filesearch("f:\\my_proj", fvec);
	for (vector<ff>::size_type i = 0; i < fvec.size(); i++)
	{
		fprintf(in, "%s\n", fvec[i].p);
	}
	fclose(in);
	system("start 1.txt");

}
walker沃克 2014-05-08
  • 打赏
  • 举报
回复

void  filesearch(char newpath[_MAX_PATH],   vector<ff> &v)
{
}
hemmingway 2014-05-08
  • 打赏
  • 举报
回复
look一下

69,374

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧