518
技術社區[雲棲]
【小程序】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