【发布时间】:2020-08-14 17:17:39
【问题描述】:
代码如下:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
using namespace std;
const char alphanum[] = "0123456789!@#$%^&*abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int string_length = sizeof(alphanum)-1;
int main()
{
system("Color 0A");
int n;
cout<<"Quanto deve essere lunga la password? "; // Here it's asking me the length of the password
cin>>n;
srand(time(0));
for(int i=0; i<n; i++)
cout << alphanum[rand() % string_length];
ofstream fout("password.txt");
cout <<""<<endl; // Here I need the variable to write the password in the text file
fout.close();
fout<<"Grazie comunque"<<endl;
return 0;
}
【问题讨论】:
-
您没有存储随机生成的密码,因此您没有任何内容可放入文件中。您可以在循环中 cout 的同时将 1 个字符写入文件。
-
我想使用字母数字变量作为密码。
标签: c++ fstream alphanumeric