【问题标题】:I have to put an alphanumeric password in a text file我必须在文本文件中输入字母数字密码
【发布时间】: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


【解决方案1】:

你需要在循环内移动密码编写代码

ofstream fout("password.txt");
for(int i=0; i<n; i++)
{
    char password_char = alphanum[rand() % string_length];
    cout << password_char;
    fout << password_char;
}
fout << endl;
fout.close();

与您写入cout 的代码完全相同。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-30
    • 2012-08-16
    • 2014-03-23
    相关资源
    最近更新 更多