البحث عن الملفات في c++
-
05-07-2019 - |
سؤال
بدأت للتو تعلم c++ و أنا حاليا باستخدام codeblocks.أريد أن أكتب أحد التطبيقات التي يمكن البحث عن الملفات في الدليل بما انها subdirs, ولكن أنا لا يمكن أن يبدو للعثور على أي أمثلة جيدة لهذا و قرأت في مكان ما أن هذا غير ممكن إلا من خلال مكتبة مثل دفعة.
- هل هذا صحيح ؟
- أي أمثلة لفعل ذلك دون المكتبة ؟
شكرا مقدما
المحلول
ومن الممكن أيضا استخدام ذلك باستخدام نظام OS المكالمات ، readdir على لينكس على سبيل المثال.دفعة (وغيرها من المكتبات) سوف تسمح لك لكتابة رمز المحمولة لعدة ( ؟ ) انظمة التشغيل.
هنا يمكنك العثور على وضع أمثلة http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1046380353&id=1044780608
نصائح أخرى
نعم هذا صحيح:معيار C++ لا APIs for سرد مضمون الدليل.
فإما أن تفعل ذلك من خلال مكتبة (دفعة أو غيرها) أو من خلال المكالمات المحلية على نظام التشغيل الذي تستخدمه (FindFirstFile/FindNextFile على ويندوز ، opendir/readdir على Unix).الآخرين إجابات لها رمز المثال.
ولكن إذا كنت بدأت للتو تعلم C++ فمن الأفضل أن لا صراع مع هذه الآن.التركيز على التعلم معيار C++ بداية.كتابة التطبيق كان لديك في الاعتبار هو ممارسة مفيدة ولكن أود أن تأجيله قليلا حتى بعد أن كنت أكثر دراية مع معيار C++.
تعزيز ليس فقط طريقة مسح الدلائل ولكن ربما أسهل طريقة للقيام بذلك في منصة محايدة الطريق - أيدون استخدام منصة محددة API (مثل Win32).
وهنا مثال بسيط باستخدام دفعة:
#include <boost/filesystem.hpp>
#include <iostream>
#include <iterator>
#include <set>
std::set<boost::filesystem::path> getDirContents (const std::string& dirName)
{
std::set<boost::filesystem::path> paths;
std::copy
( boost::filesystem::directory_iterator (dirName)
, boost::filesystem::directory_iterator ()
, std::inserter (paths, paths.end ())
);
return paths;
}
int main (int argc, char* argv[])
{
std::set<boost::filesystem::path> paths = getDirContents ("C:\\");
std::copy
( paths.begin ()
, paths.end ()
, std::ostream_iterator<boost::filesystem::path> (std::cout, "\n")
);
return 0;
}
هذا البرنامج النصي يستخدم لجمع الملفات إنشاء وتعديل أحدث المعلومات على النظام الخاص بك محرك الأقراص يمكننا البحث عن أي تاريخ معرفة نتائج جميع الإخراج سيتم تخزينها في ملف ثابت ، لذلك أسهل لعرض السجل.قبل تشغيل هذا البرنامج ، يرجى تأكيد إذن القراءة/الكتابة من أجل إخراج ملف ثابت (FileList.txt).
فإنه يساعد على معرفة كل ما يتم إنشاء ملفات/تحديث على تلك التي تعطى البحث الآن.معظم البرامج الخبيثة محاولة لحقن/يؤثر على الملفات, لذا فهو يساعد على تصحيح تلك الملفات المتأثرة.كتبت هذا البرنامج النصي باستخدام Borland C++ الإصدار 5.5 تحت منصة ويندوز.
يرجى الاطلاع على التعليمات البرمجية المصدر من بلدي بلوق التكنولوجيا - http://www.algonuts.info/how-to-trace-file-creation-and-modification-date-using-c-programming.html
#include<iostream.h>
#include<conio.h>
#include<dirent.h>
#include<dir.h>
#include<process.h>
#include<string.h>
#include<stdio.h>
#include<io.h>
#include<dos.h>
#include<sys/stat.h>
int found=0;
struct ffblk vfile;
unsigned long int udata;
char ch,present[MAXPATH];
int year,month,day;
int next_directory(char *);
void scan_directory(char *);
void main(int account,char *arg[],char *env[])
{
clrscr();
getcwd(present,MAXPATH);
DIR *dir;
struct dirent *temp;
cout<<"\nWelcome to Finder for file view";
cout<<"\n\nEnter drive:";
cin>>ch;
char base[]="X:\\";
base[0]=ch;
if ((dir = opendir(base)) == NULL)
{
clrscr();
cout<<"\nError : Derive not found";
getch();
exit(0);
}
else
{
if(access("FileList.txt",0)==0)
unlink("FileList.txt");
cout<<"\n\n\nScan Date Details:";
cout<<"\n\nEnter Year:";
cin>>year;
cout<<"\nEnter Month:";
cin>>month;
if( !(month>=1 && month<=12) )
{
clrscr();
cout<<"\nError:Value of month is not exist";
getch();
exit(0);
}
cout<<"\nEnter Day:";
cin>>day;
if( !(day>=1 && day<=31) )
{
clrscr();
cout<<"\nError:Value of day is not exist";
getch();
exit(0);
}
}
scan_directory(base);
while ((temp = readdir(dir)) != NULL)
{
char *directory = (char *) malloc(3+strlen(temp->d_name)+1);
strcpy(directory,base);
strcat(directory,temp->d_name);
next_directory(directory);
free(directory);
}
closedir(dir);
clrscr();
if(access("FileList.txt",0)==0)
cout<<"\n\nSystem: Successfully Find all files and stored in FileList.txt";
else
cout<<"\n\nSystem: No file has Modified on"<<day<<"//"<<month<<"//"<<year;
sleep(3);
}
int next_directory(char *path)
{
int count=0;
DIR *dirtemp;
char *hold,*temp;
struct dirent *ptemp;
hold=path;
if ((dirtemp = opendir(path)) != NULL)
scan_directory(path);
else
return 0;
while ((ptemp = readdir(dirtemp)) != NULL)
{
char *directory = (char *) malloc(1+strlen(ptemp->d_name)+1);
directory[0]='\\';
strcpy(directory+1,ptemp->d_name);
if(directory[1]!='\.')
{
count=strlen(hold);
temp = (char *) malloc(strlen(hold)+strlen(directory)+1);
strcpy(temp,hold);
strcat(temp,directory);
free(directory);
if(opendir(temp)!=NULL)
next_directory(temp);
temp[count]='\0';
free(temp+count+1);
hold=temp;
}
else
free(directory);
}
closedir(dirtemp);
return 0;
}
void scan_directory(char *tempo)
{
cout<<"\n"<<tempo;
FILE *stream;
struct ftime ft;
if(present[0]==tempo[0])
chdir(tempo);
else
{
setdisk(tempo[0]-65);
chdir(tempo);
}
udata = findfirst("*.*",&vfile,0x02);
while (!udata)
{
stream = fopen(vfile.ff_name,"r");
if(stream==NULL)
{
fclose(stream);
udata=findnext(&vfile);
continue;
}
else
{
getftime(fileno(stream), &ft);
if(year==ft.ft_year+1980 && month==ft.ft_month && day==ft.ft_day)
{
//cout<<"\n"<<tempo<<" "<<vfile.ff_name<<" Hour:"<<ft.ft_hour<<" Min:"<<ft.ft_min<<" Sec:"<<ft.ft_tsec *2;
if(present[0]==tempo[0])
system("cd\\");
chdir(present);
char string[25];
FILE *fp = fopen("FileList.txt","a+");
if(found==0)
{
fputs("Scan Date: ",fp);
itoa(ft.ft_day, string, 10);
fputs(string,fp);
fputc('\\',fp);
itoa(ft.ft_month, string, 10);
fputs(string,fp);
fputc('\\',fp);
itoa((ft.ft_year+1980), string, 10);
fputs(string,fp);
fputs("\n\n",fp);
found=1;
}
unsigned get;
_dos_getfileattr(vfile.ff_name,&get);
if (get & _A_HIDDEN)
fputs("[Hidden File] ",fp);
get=0;
if(ft.ft_hour<10)
get=1;
itoa(ft.ft_hour, string, 10);
fputs(string,fp);
fputc('\\',fp);
if(ft.ft_min<10)
get=get+1;
itoa(ft.ft_min, string, 10);
fputs(string,fp);
fputc('\\',fp);
if(ft.ft_tsec<10)
get=get+1;
itoa(ft.ft_tsec, string, 10);
fputs(string,fp);
for(int count=0;count<get;count++)
fputs(" ",fp);
fputs(" ",fp);
fputs(tempo,fp);
fputs(" ",fp);
fputs(vfile.ff_name,fp);
fputc('\n',fp);
fclose(fp);
if(present[0]==tempo[0])
chdir(tempo);
else
{
setdisk(tempo[0]-65);
chdir(tempo);
}
}
fclose(stream);
}
udata=findnext(&vfile);
}
if(present[0]==tempo[0])
system("cd\\");
chdir(present);
}