#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
*/