Display Directory

I have made a simple program that displays the directory but i just want it to display the files with the extention .txt Does anyone know how to modify this to make it so it does that instead of displaying the whole directory

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int ListFiles()
{
    system("cls");
    LetterByLetter Print;
    DisplayText = "Your Current Directory:";
    Print.Text();
    cout << endl;
    cout << endl;
    cout << "<--------Your Database-------->" << endl;
    DIR*     dir;
    dirent*  pdir;
    dir = opendir(".");    
    while (pdir = readdir(dir)) 
    {
                          
        cout << pdir->d_name << endl;
        

    }
    cout << "<----------------------------->" << endl;
    closedir(dir);
    DisplayText = "Press (Enter) To Go Back To Main Menu";
    Print.Text();
    cin.get();
Check to see if the last four characters in the filename are ".txt", and only print the name if they are.

1
2
3
4
5
6
7
        string name = pdir->d_name;  // the file name
        name.erase( 0, name.find_last_of( '.' ) );  // the last four digits of the file name
        transform( name.begin(), name.end(), name.begin(), ptr_fun <int, int> ( tolower ) );  // make it lowercase
        if (name == ".txt")
        {
            cout << pdir->d_name << endl;
        }

Hope this helps.
Last edited on
Where would i put this in the current function???
you would replace lines 15 to 18 with the lines that Duoas has provided. It needs to go in a loop.
I tried it and when i test the function out it just says .txt as the output. It doesn't display the files with the extention .txt. I want to display whats before the .txt :P
Last edited on
Oops! Fixed above.
I was working on the same thing last night. This was my solution:
http://cplusplus.com/forum/general/61834/
Topic archived. No new replies allowed.