I'm making a login system which will have saved usernames and passwords in a binary file so if any username or password is matched, it should show "matched!" it works fine with txt file but giving segmentation faults in binary mode
I tried to put f.read() outside the loop and read it manually. i also tried to terminate loop when !f.eof() becomes true but in all cases the same problem happens
here is the code
#include<iostream>
#include<fstream>
using namespace std;
class Login {
public:
string username;
int pass;
Login() {
}
Login(string u,int p) {
username = u;
pass =p;
}
};
int main() {
fstream f("sarah.bin",ios::in|ios::app|ios::binary);
if(!f) cout<<"error"<<endl;
//adding data,take data from user
Login l("ahmed",123),k;
f.write((char*)&l,sizeof(l));
bool logged = false;
string s = "ahmed";
int p = 123;
f.seekg(0,ios::beg);
//verification
while(!f.eof() && f.read((char*)&k,sizeof(l))) {
if(k.pass==p && k.username == s) {
cout<<"logged";
logged = true;
break;
}
}
if(!logged)
cout<<"not matched";
f.close();
}
It is not giving expected result but giving segmentation fault and sometimes no output at all.