Picking a random text file from a folder

So I made a random weapon generator and it creates a text file containing the information on the weapon it made

What i now want to do is write another program that picks a random one of the .txt files and puts the info into a variable so i can use it in a weapon shop game the problem is I have absolutely no idea what to do

searching for a long while i found these things i may need but im clueless

FindFirstFile
FindNextFile
FindClose
and WIN32_FIND_DATA structure
if it is CLI, then type the file names in an array, then use rand();...
something of that sort.

eg:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    char* filenames[]={"test.txt","help.txt"};
    int random;
    srand(time(NULL));

    random=rand()%2+0;
    if(random==0)
        cout<<filenames[random]<<" chosen!";
    else if(random==1)
        cout<<filenames[random]<<" chosen!";
    cin.get();
    return 0;
}


Hope it HELPS!!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <cstdlib>
#include <fstream>
#include <string>
#include <iostream>
#include <ctime>

int main(void) {
      srand(time(NULL));
      char *filenames[]={ "test.txt","help.txt" };
      std::ifstream readFile(filenames[rand() % 2]);
      std::string data;
      getline(readFile, data, '\0');
      std::cout << data;
      return 0;
}

You could do something like this.
Last edited on
> searching for a long while i found these things i may need but im clueless
> FindFirstFile FindNextFile FindClose and WIN32_FIND_DATA structure

Just use them together. For instance:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <windows.h>
#include <string>
#include <random>
#include <ctime>

inline bool ends_with( const std::string str, const std::string& suffix )
{
    auto suf_sz = suffix.size() ;
    auto str_sz = str.size() ;
    return ( suf_sz <= str_sz ) && ( str.substr(str_sz-suf_sz) == suffix ) ;
}

std::string pick_random_txt_file( std::string path )
{
    static const std::string filter = "*.txt" ;
    if( !ends_with( path, filter ) )
    {
        static const std::string seperator = "\\" ;
        if( !ends_with( path, seperator ) ) path += seperator ;
        path += filter ;
    }

    std::string selected_file ;
    int n = 0 ;
    WIN32_FIND_DATAA fdata ;

    HANDLE hf = FindFirstFileA( path.c_str(), &fdata ) ;
    if( hf != INVALID_HANDLE_VALUE )
    {
        do
        {
            static std::mt19937 rng( std::time(nullptr) ) ;
            if( !( fdata.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ) &&
                ( std::uniform_int_distribution<>( 1, ++n )(rng) == 1 ) )
                    selected_file = fdata.cFileName ; // replace with probability 1/n
        } while( FindNextFile( hf, &fdata ) ) ;

        FindClose(hf) ;
    }

    return selected_file ;
}
Topic archived. No new replies allowed.