Top.Mail.Ru
Ответы

В консоли выдает иероглифы вместо паролей

учу язык си, сделал генератор паролей, все кажется работает, но в консоли и файле выдает иероглифы вместо паролей что делать сам код:

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
 #include <stdio.h> 
#include <stdlib.h> 
#include <locale.h> 
#include <time.h> 
#include <string.h> 
 
void generate_password(char *pass) { 
    const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()"; 
    srand(time(NULL)); 
 
    int length = (rand() % 16) + 5; 
 
    for (int i = 0; i < length; i++) { 
        pass[i] += chars[rand() % (sizeof(chars) - 1)]; 
    } 
    pass[length] = '\0'; 
} 
 
int main() { 
    srand(time(NULL)); 
 
    int colvo; 
    char password[16]; 
 
    printf("Select the required number of passwords: "); 
    while(scanf("%d", &colvo) != 1){ 
        printf("\nError: Please enter a positive number..."); 
        printf("\nSelect the required number of passwords: "); 
        while(getchar() != '\n'); 
    } 
 
    FILE *file = fopen("C:\\Users\\тимур\\Desktop\\passwords.txt", "a"); 
    if (file == NULL) { 
        printf("\nError opening file!"); 
        return 1; 
    } 
 
    printf("\nPasswords:"); 
    fprintf(file, "Passwords:"); 
 
    for(int i = 0; i <= colvo - 1; i++){ 
        generate_password(password); 
        printf("    \n%d password: %s", i + 1, password); 
        fprintf(file, "    \n%d password: %s", i + 1, password); 
    } 
 
    fprintf(file, "\n\n"); 
    fclose(file); 
 
    return 0; 
}  
Дополнен

#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <time.h>
#include <string.h>

void generate_password(char *pass) {
const char chars[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()";
srand(time(NULL));

int length = (rand() % 16) + 5;

for (int i = 0; i < length; i++) {
pass[i] += chars[rand() % (sizeof(chars) - 1)];
}
pass[length] = '\0';
}

int main() {
srand(time(NULL));

int colvo;
char password[16];

printf("Select the required number of passwords: ");
while(scanf("%d", &colvo) != 1){
printf("\nError: Please enter a positive number...");
printf("\nSelect the required number of passwords: ");
while(getchar() != '\n');
}

FILE *file = fopen("C:\\Users\\тимур\\Desktop\\passwords.txt", "a");
if (file == NULL) {
printf("\nError opening file!");
return 1;
}

printf("\nPasswords:");
fprintf(file, "Passwords:");

for(int i = 0; i <= colvo - 1; i++){
generate_password(password);
printf(" \n%d password: %s", i + 1, password);
fprintf(file, " \n%d password: %s", i + 1, password);
}

fprintf(file, "\n\n");
fclose(file);

return 0;
}

По дате
По Рейтингу
Аватар пользователя
Высший разум
1мес
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
 // C++20
#include <algorithm> 
#include <format> 
#include <iostream> 
#include <random> 
#include <ranges> 
#include <string> 
#include <unordered_set>

using namespace std;

class GenPassword { 
    inline static const string pattern{ 
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()" 
    }; 
    static bool is_reliable(const string_view& password) { 
        static const unordered_set<char> lowers{ pattern.begin(), pattern.begin() + 26 }; 
        static const unordered_set<char> uppers{ pattern.begin() + 26, pattern.begin() + 52 }; 
        static const unordered_set<char> digits{ pattern.begin() + 52, pattern.begin() + 62 }; 
        static const unordered_set<char> puncts{ pattern.begin() + 62, pattern.end() }; 
        return ranges::any_of(password, [](char ch) { return digits.contains(ch); }) 
            && ranges::any_of(password, [](char ch) { return puncts.contains(ch); }) 
            && ranges::any_of(password, [](char ch) { return lowers.contains(ch); }) 
            && ranges::any_of(password, [](char ch) { return uppers.contains(ch); }); 
    } 
public: 
    static string generate(size_t length) { 
        uniform_int_distribution<size_t> uid(0, pattern.size() - 1); 
        mt19937 gen{ random_device()() }; 
        if (length < 6) length = 6; 
        string password; 
        do { 
            password.clear(); 
            for (size_t i = 0; i < length; ++i) password += pattern[uid(gen)]; 
        } while (!is_reliable(password)); 
        return password; 
    } 
};

int main() { 
    uniform_int_distribution<size_t> uid(6, 15); 
    mt19937 gen{ random_device()() }; 
    string password; 
    string buffer; 
    for (size_t i = 0; i < 25; ++i) { 
        password = GenPassword::generate(uid(gen)); 
        format_to(back_inserter(buffer), "{:>2}. {}\n", i + 1, password); 
    } 
    cout << buffer << '\n'; 
    system("pause > nul"); 
} 
Аватар пользователя
Просветленный
1мес

Кажется, дело в кодировке. Надо попробовать у окна командной строки в настройках установить ту же кодировку, что в исходника (и у файла passwords.txt) и запустить исполняемый файл из командной строки.

Аватар пользователя
Профи
1мес

Надо добавить в int main() следующий код:

1
 system("chcp 1252 > nul") 
Аватар пользователя
Мастер
1мес

Я пока не могу помочь вам с решением этой проблемы.

Аватар пользователя
Мудрец
1мес

Это я выдаю, не мешай