В консоли выдает иероглифы вместо паролей
учу язык си, сделал генератор паролей, все кажется работает, но в консоли и файле выдает иероглифы вместо паролей что делать сам код:
#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;
}
// 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");
}
Кажется, дело в кодировке. Надо попробовать у окна командной строки в настройках установить ту же кодировку, что в исходника (и у файла passwords.txt) и запустить исполняемый файл из командной строки.
Надо добавить в int main() следующий код:
system("chcp 1252 > nul")
Я пока не могу помочь вам с решением этой проблемы.
Это я выдаю, не мешай