我刚开始学习c ++,目前正在使用代码块。 我想写一个应用程序,可以搜索目录中的文件,包括它的子目录,但我似乎无法找到任何好的例子,我在某处读到这只能通过像boost这样的库。

  1. 这是真的吗?
  2. 没有库的任何例子吗?
  3. 提前致谢

有帮助吗?

解决方案

也可以使用OS系统调用,例如linux上的readdir。 boost(和其他库)将允许您为几个(所有?)操作系统编写可移植代码。

在这里,您可以找到精心设计的示例 http:// faq .cprogramming.com /的cgi-bin / smartfaq.cgi回答= 1046380353&安培;?ID = 1044780608

其他提示

是的,确实如此:标准C ++没有用于列出目录内容的API。

所以你要么通过库(Boost或其他)或通过你正在使用的操作系统的本机调用(Windows上的FindFirstFile / FindNextFile,Unix上的opendir / readdir)。其他答案有示例代码。

但是如果你刚刚开始学习C ++,那么现在最好不要与它们斗争。一开始就专注于学习标准C ++。编写你想到的应用程序是一个有用的练习,但是在你更熟悉标准C ++之后我会推迟一点。

Boost不是扫描目录的唯一方法,但它可能是以平台中立方式执行此操作的最简单方法 - 即不使用特定于平台的API(例如Win32)。

以下是使用boost的简单示例:

#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)的读/写权限。

有助于了解在给定的搜索日期创建/更新所有文件的内容。大多数恶意软件程序都会尝试注入/影响文件,因此有助于调试受影响的文件。我在Windows平台下使用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);
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top