C++ STL 快速生成带后缀的随机文件名

如果你不想使用系统API或者API不可用,那么可使用以下类似代码代替随机文件名的生成,代码生成4到32位长度的随机文件名,并以.txt结尾。
#include <string>
#include <random>
static std::string random_name()
{
	const char cs[] = "0a{1b2c3d$4e5f6g7h_8@i9jAkBl`CmDnEo[FpGqHrI~s-JtKuLvMw%NxOyPz(Q9R8S7T6)U5V4]W3X2}Y1Z0";
	std::random_device r;
	std::default_random_engine e(r());
	std::uniform_int_distribution<> u(0, _countof(cs) - 2);

	char n[((_countof(cs) - 2) + 12) / 3 + _countof(".txt")];
	int l = (u(e) + 12) / 3; // length [4, 32]
	strcpy(&n[l], ".txt");
	while (--l >= 0) {
		n[l] = cs[u(e)];
	}
	return n;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值