I'm currently creating a custom file structure (File extension is .ndev), to increase my skill in working with files in C++. I can save values to a file in a specific way (See below)
{
"username": "Nikkie",
"password": "test",
"role": "Developer",
"email": "test@gmail.com"
}
This doesn't have anything to do with actual JSON, it's just structured like it.
My question is, how can I read the value of one of those variables with C++, without it coming out like the screenshot below:
My current code to write the file:
void user::RegisterUser(string username, string password, string role, string email)
{
string filename = "E:\\Coding\\C\\test\\data\\" + username + ".ndev";
ifstream CheckFile(filename);
if (CheckFile.good())
{
printf("User already exists!");
}
else {
ofstream UserDataFile(filename);
UserDataFile << "{\n\t\"username\": \"" << username << "\",\n\t\"password\": \"" << password << "\",\n\t\"role\": \"" << role << "\",\n\t\"email\": \"" << email << "\"\n}";
UserDataFile.close();
}
CheckFile.close();
}
Don't bludgeon me about the password encryption, I will add that later. I'm currently trying to actually let it read the values before I do anything else
My current code to read the file:
void user::LoginUser(string username)
{
string filename = "E:/Coding/C/test/data/" + username + ".ndev";
ifstream UserFile(filename, ios_base::in);
if (UserFile.good())
{
string name;
string passw;
string role;
string email;
while (UserFile >> name >> passw >> role >> email)
{
cout << name << passw << endl;
cout << role << email << endl;
}
}
else
{
printf("User doesn't exist!");
}
}
I just can't seem to get it to display the values properly, there are also no errors listed in the console nor in the VS debug build.