1

This is a function of my code. Whenever, i am loging in as any user, it,s fine for the first time. But, from the second time it doesn't fetch the string value from the file to match with the password provided by the user.

    int userLogin(){
        int studentUser;
string studentPassword;
int x=1;
system("cls");
cout<<"\n ---------- Student Login -------";
cout<<"\n\n Enter username: ";
cin>>studentUser;
cout<<"\n Enter password:";
cin>>studentPassword;
char filename[20];
sprintf(filename,"%d%s",studentUser,".dat");
ifstream fin(filename,ios::in);
student t;
fin.read((char*)&t,sizeof(t));
if(t.PASSWORD==studentPassword){
while (x!=0){
    int innerChoice = 0;
    while(innerChoice != 9){
    system("cls");}}}

for every time user should give the username and password to login. using the username the file will be located , then password from the file will be matched with the entered password.

sandy
  • 23
  • 4
  • 1
    Thank you for posting a question. Please include a main() function and provide a [Compilable, Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve). This helps us help you. If you can make your problem as simple as possible while still creating the failure, it will help us isolate the issues that will make your code work correctly. By providing a main() function, it saves time for the person trying to duplicate your error and help you. – Gardener Mar 24 '19 at 12:07
  • 2
    You should check whether the file is actually opened after opening it. **I would also like to point out that using plaintext passwords is extremely unsafe and must never be done**. – Simon Doppler Mar 24 '19 at 12:09
  • file is opening , thats why it works for the first time. – sandy Mar 24 '19 at 12:12
  • @sandy this is a problem known as serialization, i.e. transfering in-memory objects to/from a stream. If you look at the duplicate or google for the term, you'll find plenty of explanations about why it doesn't work and what are the alternatives. Unrelated: I can only reinforce the warning from Simon: never ever store a password in clear text. The good practice is to store a (salted) hash of the password. Checking the password is then done by calculating the hash and comparing it with the stored hash. – Christophe Mar 24 '19 at 12:43
  • Thanks people, i will explore about it. – sandy Mar 24 '19 at 13:00

1 Answers1

1

You cannot save C++ strings in a file using write or read them back in using read. Only very specific types can be serialized this way, see POD types for the full details.

This simplest way to achieve way you want is to switch to using formatted I/O for reading and writing your Student type.

For more specific advice please post details of your Student class.

john
  • 85,011
  • 4
  • 57
  • 81
  • class student{ public: string NAME; int USER; string PASSWORD; int N; // No. of books issued setting(string name,int user,string password,int n) { NAME = name; USER = user; PASSWORD = password; N=n; } }; – sandy Mar 24 '19 at 12:15
  • So have you tried overloading `operator>>` and `operator<<` for that class? This is the normal way to do this. If you really need unformatted I/O then you have quite a lot more work to do. – john Mar 24 '19 at 12:18
  • What I'm saying is that with this class you can forget about `fin.read((char*)&t,sizeof(t));` because your class is not a POD type. You have to find some other way to do the I/O. There are lots of different ways but I'm not sure what your particular requirements are. – john Mar 24 '19 at 12:20