阅读518 返回首页    go 阿里云 go 技术社区[云栖]


【小程序】findobj

为了方便开发,实验室的师哥给了一个小任务,写一个小程序,完成以下功能:给一个txt文档,里面有一些文件名,这些文件是要求找出的;给一个目录路径,里面可能包含这个txt中指定的文件(也可能没有);如果某个文件存在就把它复制到另一个指定文件夹里面。


写了一个80来行的程序,程序木有界面,缺点是不能遍历指定文件夹中的嵌套文件夹。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<io.h>

char tFPath[60];
char dFPath[20];

bool findFiles(char *fileName)
{
	long Handle;
	struct _finddata_t FileInfo;
	char path[60];
	
	strcpy(path,tFPath);
	strcat(path,"\\");
	strcat(path,fileName);
	
	printf("path == %s\n",path);
	
	if((Handle=_findfirst(path,&FileInfo))==-1L)
	{
		printf("sorry, no such file!\n");
		return false;
	}
	
	else
	{
		printf("Find it:%s\n",FileInfo.name);
		
		char SysOrder[100]="copy ";
		strcat(SysOrder,path);
		strcat(SysOrder," ");
		strcat(SysOrder,dFPath);

		system(SysOrder);

		_findclose(Handle);
		return true;
	}
	
	return true;
}


int main()
{
	FILE *fp=NULL;
	char fileName[30];
	char a;

	char txtSrc[30];
	printf("Please enter the TXT Source file path:\n");
	scanf("%s",txtSrc);
	
	fp=fopen(txtSrc,"r");

	printf("Please enter the target folder path:\n");
	scanf("%s",tFPath);

	printf("Please enter the destination folder path:\n");
	scanf("%s",dFPath);
	
	int i=0;
	do
	{
		a=fgetc(fp);
		
		if (a!='\n')
			fileName[i++]=a;
		else
		{
			fileName[i]='\0';
			findFiles(fileName);
			i=0;
		}
		
	}while(a!=EOF);
	
	fclose(fp);

	getchar();
	
	return 0;
}


最后更新:2017-04-03 05:38:56

  上一篇:go libpcre.so.0 被删除怎么办?
  下一篇:go HDU4772-Zhuge Liang&#39;s Password