2

I need to make a program that stores usernames and passwords. I want to store them in json file. I already made a program that kinda does that, but it deletes old inputs and rewrite them as new.

int main()
{
char pass[12];
char user[12];

std::ofstream o;

o.open("logins.json");

json j;

    system("cls");
    std::cout << "Username: ";
    std::cin >> user;
    std::cout << "Password: ";
    std::cin >> pass;

    j[user]["Username"] = user;
    j[user]["Password"] = pass;

    o << std::setw(4) << j << std::endl;
}

So for example we input username: admin, password: admin. It creates a json file and stores them:

{
"admin": {
    "Username": "admin",
    "Password": "admin"
}

}

But when I run the program again and this time input username:user, password:user, it replaces admin with user. So basically it stores only one input. But I need it to store all of them. So I later can access them.

And that's not good. Can you please help me fix this or suggest something else?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 5
    You create a new json document each time you run the program, which will be empty. If you want to append info to an existing document, you need to read it first! – perivesta Jan 23 '19 at 10:18
  • 2
    Because you asked it to, try this instead `o.open("logins.json", std::ios_base:app| std::ios_base::ate);` But if you want to have a single JSON object in the output file then you are going to have to read the file first, like @dave said. Remember computers do what you tell them to, not what you want them to. – john Jan 23 '19 at 10:19
  • Okey, so i replace o.open("logins.json") with o.open("logins.json", std::ios_base:app| std::ios_base::ate) and it should work ? – Reinis Zīverts Jan 23 '19 at 10:27
  • @ReinisZīverts That completely depends on what you mean by work. You will not see the new output replace the old output, but whether you'll be happy or not I can't say, since I don't know exactly what you want. – john Jan 23 '19 at 10:32
  • @john Well, thats all I need. So each time I run the programm it adds new data to json file, depending on input, but the old data still stays there. – Reinis Zīverts Jan 23 '19 at 10:38
  • Storing passwords? Yikes! – Lightness Races in Orbit Jan 23 '19 at 12:17
  • @LightnessRacesinOrbit It's just for school, no actual passwords will be stored that way. – Reinis Zīverts Jan 23 '19 at 13:56
  • Hopefully your teacher is not teaching you to store passwords! – Lightness Races in Orbit Jan 23 '19 at 14:14

1 Answers1

1

Every time you run this application it rewrites file logins.json ignoring its existing contents.

If you would like to edit the json file, you need to load it first, decode json, modify json, and serialize the json back into the file overwriting it.

E.g.:

int main() {
    json j;
    {
        std::ifstream i("logins.json");
        if(i.is_open())
            i >> j;
    }

    system("cls");
    std::string pass;
    std::string user;
    std::cout << "Username: ";
    std::cin >> user;
    std::cout << "Password: ";
    std::cin >> pass;

    j[user]["Username"] = user;
    j[user]["Password"] = pass;

    std::ofstream o("logins.json");
    o << std::setw(4) << j << '\n';
}
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • Could you help me with the code. This is all new stuff to me, but I need to do it for class project, and i've searched alot, I think it is around 10H of solid searching for answers, but all I can find is some code bits, but not the whole picture. And I can't put it all together, as you can see. – Reinis Zīverts Jan 23 '19 at 10:36
  • @ReinisZīverts Added an example for you. – Maxim Egorushkin Jan 23 '19 at 10:36
  • OMG. Thank you so much. You just saved my day and my sanity. – Reinis Zīverts Jan 23 '19 at 10:45