Как записать данные из массива в файл с++
//Как записать данные из массива в файл с++
//У меня то не записывается не чего, то какие - то иероглифы
//нужна помощь с тем как записать данные из массива char в файл
void registration() {
char username[20];
char password[20];
bool match = 0;
bool successful = 0;
do//Create a password, check if the user entered the same password twice.
{
system("cls");
cout << "\t\t\t\t_________________________Registration_________________________" << endl;
cout << "\t\t\t\tInput UserName: ";
cin >> username;
cout << "\t\t\t\tInput Password: ";
cin >> password;
cout << "\t\t\t\tConfirm Input Password: ";
string password_r = " ";
cin >> password_r;
if (password_r == password)
successful = 1;
else
{
cout << endl << "TWO PASSWORDS DID NOT MATCH, TRY AGAIN." << endl <<
"ENTER 1 TO CONTINUE." << endl;;
bool x = 0;
cin >> x;
if (x == 1)
registration();
}
} while (successful == 0);
FILE* FileIn = fopen("records.txt", "a");
char(username);
char(password);
char(fwrite(username, sizeof(char), 10, FileIn));
fwrite(password, sizeof(char), 10, FileIn);
match = 1;
fclose(FileIn);
}
Ответы (2 шт):
Автор решения: Harry
→ Ссылка
Вы хотите записать как бинарные данные, или как текстовые? Если бинарные — то записями по 20 байт или нет?
Если же простым текстом, то так же, как на консоль — только в файл :)
fprintf(FileIn,"%s\n%s\n",username,password);
Автор решения: Andrei
→ Ссылка
У меня получилось Вот так это сделать:
#include <iostream>
#include <fstream>
void registration();
int main(int argc, char *argv[])
{
registration();
return 0;
}
void registration() {
char username[20];
char password[20];
bool match = 0;
bool successful = 0;
do//Create a password, check if the user entered the same password twice.
{
system("cls");
std::cout << "\t\t\t\t_________________________Registration_________________________" << std::endl;
std::cout << "\t\t\t\tInput UserName: ";
std::cin >> username;
std::cout << "\t\t\t\tInput Password: ";
std::cin >> password;
std::cout << "\t\t\t\tConfirm Input Password: ";
std::string password_r = " ";
std::cin >> password_r;
if (password_r == password)
successful = 1;
else
{
std::cout << std::endl << "TWO PASSWORDS DID NOT MATCH, TRY AGAIN." << std::endl <<
"ENTER 1 TO CONTINUE." << std::endl;;
bool x = 0;
std::cin >> x;
if (x == 1)
registration();
}
} while (successful == 0);
std::ofstream Fout;
Fout.open("records.txt", std::ofstream::app);
if (!Fout.is_open())
{
std::cout << "Что-то пошло не так..." << std::endl;
}
else
{
Fout << username << " : " << password << std::endl;
}
Fout.close();
}