Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string >
- #include <fstream >
- struct User
- {
- std::string username; // this is the problem
- // char username[50]; // this works fine
- int password;
- };
- int main()
- {
- std::ofstream out("data.dat", std::ios::binary | std::ios::out | std::ios::app);
- User theUser;
- std::cout << "username: ";
- std::getline(std::cin, theUser.username);
- std::cin.ignore(1, 'n');
- std::cout << "password: ";
- std::cin >> theUser.password;
- std::cout << std::endl;
- std::cin.ignore(1, 'n');
- out.write((char*)&theUser, sizeof(theUser));
- out.flush();
- out.close();
- std::ifstream in("data.dat", std::ios::binary | std::ios::in);
- User theUser2;
- in.seekg(0, std::ios::end);
- int nUser = in.tellg() / sizeof(User);
- in.clear();
- in.seekg(0, std::ios::beg);
- std::cout << "nUser: " << nUser << std::endl;
- std::cout << "Reading now...nn";
- for(int i(0); i < nUser; i++)
- {
- if(in.is_open())
- {
- in.read((char*)&theUser2, sizeof(User));
- std::cout << "username: " << theUser2.username << std::endl;
- std::cout << "password: " << theUser2.password << std::endl;
- }
- }
- in.close();
- std::cout << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement