Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,870 questions

51,793 answers

573 users

How to create a list of random file names, including extension, dates, and file size sorted by filename in c++

1 Answer

0 votes
#include <iostream>
#include <vector>
#include <string>
#include <locale>
#include <algorithm>
#include <ctime>
 
// Structure to hold file information
struct FileInfo {
    std::string name;
    std::string extension;
    std::string date;
    size_t size;
};
 
// Function to generate a random string of given length
std::string generateRandomString(size_t length) {
    const char charset[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
    std::string result;
    result.reserve(length);
    for (size_t i = 0; i < length; ++i) {
        result += charset[rand() % (sizeof(charset) - 1)];
    }
    return result;
}
 
// Function to generate a random date
std::string generateRandomDate() {
    time_t t = time(0);
    tm* now = localtime(&t);
    now->tm_year = rand() % 30 + 93; // Random year between 1990 and 2023
    now->tm_mon = rand() % 12;
    now->tm_mday = rand() % 28 + 1;
    char buffer[11];
    strftime(buffer, 11, "%Y-%m-%d", now);
    return std::string(buffer);
}
 
// Function to generate a random file size
size_t generateRandomFileSize() {
    return rand() % 100000 + 1; // Random size between 1 and 100000 bytes
}
 
int main() {
    size_t totla_files = 10;
     
    srand(time(0)); // Seed for random number generation
 
    std::vector<FileInfo> files;
    const std::vector<std::string> extensions = {".txt", ".cpp", ".jpg", ".png", ".pdf"};
 
    // Generate random files
    for (int i = 0; i < totla_files; ++i) {
        FileInfo file;
        file.name = generateRandomString(8);
        file.extension = extensions[rand() % extensions.size()];
        file.date = generateRandomDate();
        file.size = generateRandomFileSize();
        files.push_back(file);
    }
 
    // Sort files by name
    std::sort(files.begin(), files.end(), [](const FileInfo& a, const FileInfo& b) {
        return a.name < b.name;
    });
 
    std::cout.imbue(std::locale(""));
    for (const auto& file : files) {
        std::cout << file.name << file.extension << " "
                  << file.date << " "
                  << file.size << " bytes" << std::endl;
    }
}
 
 
    
/*
run:
    
8YhigyML.txt 2010-09-26 76,018 bytes
BrfUtqHO.png 1995-12-20 95,633 bytes
JfAMdZ3g.cpp 2010-01-17 22,682 bytes
daSgX0fT.txt 2021-11-27 87,754 bytes
fOEWc6S1.pdf 2009-10-16 2,131 bytes
j96l527a.pdf 2020-07-26 56,814 bytes
mOOwLkuE.txt 2009-11-20 48,437 bytes
nZuJzFoc.jpg 2022-11-17 79,386 bytes
pIZmqyrO.pdf 2015-05-08 54,903 bytes
tEHEuKiW.png 2005-08-26 41,420 bytes
    
*/

 



answered Apr 18, 2025 by avibootz
edited Apr 18, 2025 by avibootz
...