生成一个随机文件名,然后创建文件,在 C++ 中
Producing a random file name, and then creating the file, In C++
在我的程序中,我试图生成一个随机文件名,然后使用 fopen 创建一个具有该名称的文件。过程如下
- 创建一个随机文件名
- 通过尝试在 c:\
中创建具有该名称的文件来检查我们是否是管理员
- 将内容写入文件
我用来制作随机文件名的函数是:
const char *RandomName(const char *suffix,unsigned int length)
{
const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
std::string Str;
unsigned int i;
Str.append("c:\");
for( i = 0; i < length; ++i)
{
Str += alphanum[rand() % stringLength];
}
Str += suffix;
const char *str =Str.c_str();
return str;
}
我用来创建文件和检查管理员的函数是:
bool IsAdmin()
{
const char *n = RandomName(".BIN",5);
cout << n << endl;
FILE *fp;
fp = fopen((const char *)n,"w+");
if (fp == NULL) {
cout << "File pointer was NULL" << endl;
return false;
} else {
cout << "File pointer is legit" << endl;
//fclose(fp);
//remove(n);
int b;
for(b = 0; b != 1338; b++)
{
char f = 'c';
fputc(f, fp);
}
return true;
}
}
当运行作为管理员时,程序打印:
c:UswA.BIN
Not Admin!
如何让程序创建一个名称与其在屏幕上显示的名称相匹配的文件?并且没有粗略的行为?
我认为 运行以管理员身份在此处创建的 .exe 可以让您创建文件。
运行 程序不是来自任何 IDE 而是手动来自文件位置和 运行 作为管理员。
为了解决这个问题,我删除了 RandomName
函数并编辑了 IsAdmin
以包含它的代码,通过一些调整我至少可以让它工作得很好,我最终得到的代码是:
void AdminWrite(const char *suffix,unsigned int length)
{
FILE *fp;
const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv";
int stringLength = sizeof(alphanum) - 1;
std::string Str;
unsigned int i;
Str.append("c:\Users\UserName\Desktop\");
for( i = 0; i < length; ++i)
{
Str += alphanum[rand() % stringLength];
}
Str += suffix;
const char *str =Str.c_str();
cout << str << endl;
fp = fopen(str,"w+");
if (fp == NULL) {
cout << "File pointer was NULL" << endl;
return;
} else {
cout << "File pointer is legit" << endl;
int b;
for(b = 0; b != 1337; b++)
{
char f = 'c';
fputc(f, fp);
}
return;
}
return;
}
简单
只需使用 tmpnam c api
示例:
#include <stdio.h>
int main(void)
{
char name[40];
int i;
for(i=0; i<3; i++) {
tmpnam(name);
printf("%s ", name);
}
return 0;
}
在我的程序中,我试图生成一个随机文件名,然后使用 fopen 创建一个具有该名称的文件。过程如下
- 创建一个随机文件名
- 通过尝试在 c:\ 中创建具有该名称的文件来检查我们是否是管理员
- 将内容写入文件
我用来制作随机文件名的函数是:
const char *RandomName(const char *suffix,unsigned int length)
{
const char alphanum[] =
"0123456789"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz";
int stringLength = sizeof(alphanum) - 1;
std::string Str;
unsigned int i;
Str.append("c:\");
for( i = 0; i < length; ++i)
{
Str += alphanum[rand() % stringLength];
}
Str += suffix;
const char *str =Str.c_str();
return str;
}
我用来创建文件和检查管理员的函数是:
bool IsAdmin()
{
const char *n = RandomName(".BIN",5);
cout << n << endl;
FILE *fp;
fp = fopen((const char *)n,"w+");
if (fp == NULL) {
cout << "File pointer was NULL" << endl;
return false;
} else {
cout << "File pointer is legit" << endl;
//fclose(fp);
//remove(n);
int b;
for(b = 0; b != 1338; b++)
{
char f = 'c';
fputc(f, fp);
}
return true;
}
}
当运行作为管理员时,程序打印:
c:UswA.BIN Not Admin!
如何让程序创建一个名称与其在屏幕上显示的名称相匹配的文件?并且没有粗略的行为?
我认为 运行以管理员身份在此处创建的 .exe 可以让您创建文件。 运行 程序不是来自任何 IDE 而是手动来自文件位置和 运行 作为管理员。
为了解决这个问题,我删除了 RandomName
函数并编辑了 IsAdmin
以包含它的代码,通过一些调整我至少可以让它工作得很好,我最终得到的代码是:
void AdminWrite(const char *suffix,unsigned int length)
{
FILE *fp;
const char alphanum[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuv";
int stringLength = sizeof(alphanum) - 1;
std::string Str;
unsigned int i;
Str.append("c:\Users\UserName\Desktop\");
for( i = 0; i < length; ++i)
{
Str += alphanum[rand() % stringLength];
}
Str += suffix;
const char *str =Str.c_str();
cout << str << endl;
fp = fopen(str,"w+");
if (fp == NULL) {
cout << "File pointer was NULL" << endl;
return;
} else {
cout << "File pointer is legit" << endl;
int b;
for(b = 0; b != 1337; b++)
{
char f = 'c';
fputc(f, fp);
}
return;
}
return;
}
简单 只需使用 tmpnam c api
示例:
#include <stdio.h>
int main(void)
{
char name[40];
int i;
for(i=0; i<3; i++) {
tmpnam(name);
printf("%s ", name);
}
return 0;
}