C
C#•2y ago
cgxlm

Writing and Reading binary files not working

Im making a game in windows forms for school and im making a register and login screen where i make use of binary files, whenever I try to login though i just get an error that its wrong everytime. I accidentally opened the .bin file in notepad earlier which may have f'd it up im not sure
15 Replies
phaseshift
phaseshift•2y ago
notepad not going to modify it unless you added text and saved it. Show your code, show the error.
cgxlm
cgxlmOP•2y ago
if ((boolname == true)&&(boolsurname == true) && (boolemail == true) && (boolusername == true) && (boolpassword == true) && (boolconfirm == true))
{
//Creates a new entry in the binary file. 'Append' mode means that it adds the entry to the end of the file.
FileStream filestream = new FileStream("credentials.bin", FileMode.Append);
BinaryWriter binaryWriter = new BinaryWriter(filestream);

//This will write the user's valid data to the binary file.
binaryWriter.Write(name);
binaryWriter.Write(surname);
binaryWriter.Write(email);
binaryWriter.Write(username);
binaryWriter.Write(password);

//Saves and closes binary file
binaryWriter.Flush();
binaryWriter.Close();
MessageBox.Show("You have successfully registered. Please log in using the login form.", "Registration successful", MessageBoxButtons.OK);
Login loginForm = new Login();
loginForm.Show();
this.Close();
}
if ((boolname == true)&&(boolsurname == true) && (boolemail == true) && (boolusername == true) && (boolpassword == true) && (boolconfirm == true))
{
//Creates a new entry in the binary file. 'Append' mode means that it adds the entry to the end of the file.
FileStream filestream = new FileStream("credentials.bin", FileMode.Append);
BinaryWriter binaryWriter = new BinaryWriter(filestream);

//This will write the user's valid data to the binary file.
binaryWriter.Write(name);
binaryWriter.Write(surname);
binaryWriter.Write(email);
binaryWriter.Write(username);
binaryWriter.Write(password);

//Saves and closes binary file
binaryWriter.Flush();
binaryWriter.Close();
MessageBox.Show("You have successfully registered. Please log in using the login form.", "Registration successful", MessageBoxButtons.OK);
Login loginForm = new Login();
loginForm.Show();
this.Close();
}
thats the code for writing to the file
phaseshift
phaseshift•2y ago
hmm. ok.
Pobiega
Pobiega•2y ago
I would love to see the reading.
phaseshift
phaseshift•2y ago
Have yo utried writing inplain text first to get that working I don't see how you will be able to parse that when reading it, as-is
cgxlm
cgxlmOP•2y ago
public static bool CheckDetails(string username, string password)
{
FileStream fileStream = File.OpenRead("credentials.bin");
BinaryReader binaryReader = new BinaryReader(fileStream);
int arraySize = Records();

string[,] userArray = new string[arraySize, 5];


for (int x = 0; x < arraySize; x++)
{
/*This loop goes through the array and populates each row with one record.
Each row in the array contains a different detail about the user - their first name,
surname, username, password and user ID number*/
userArray[x, 0] = binaryReader.ReadString();
userArray[x, 1] = binaryReader.ReadString();
userArray[x, 2] = binaryReader.ReadString();
userArray[x, 3] = binaryReader.ReadString();
userArray[x, 4] = binaryReader.ReadString();
}

//This is used to check if the user's inputted details exist, and match any records in the file
for (int x = 0; x < arraySize; x++)
{
if (userArray[x, 2] == username)
if (userArray[x, 3] == password)
//Checks if the entered username and password are equal to those stored in the binary file
return true; //If both match, then the loop will return true.
}

binaryReader.Close();
return false; /*This will return false, as the username
and password entered do not exist in the file, or do not match any records.*/
}
public static bool CheckDetails(string username, string password)
{
FileStream fileStream = File.OpenRead("credentials.bin");
BinaryReader binaryReader = new BinaryReader(fileStream);
int arraySize = Records();

string[,] userArray = new string[arraySize, 5];


for (int x = 0; x < arraySize; x++)
{
/*This loop goes through the array and populates each row with one record.
Each row in the array contains a different detail about the user - their first name,
surname, username, password and user ID number*/
userArray[x, 0] = binaryReader.ReadString();
userArray[x, 1] = binaryReader.ReadString();
userArray[x, 2] = binaryReader.ReadString();
userArray[x, 3] = binaryReader.ReadString();
userArray[x, 4] = binaryReader.ReadString();
}

//This is used to check if the user's inputted details exist, and match any records in the file
for (int x = 0; x < arraySize; x++)
{
if (userArray[x, 2] == username)
if (userArray[x, 3] == password)
//Checks if the entered username and password are equal to those stored in the binary file
return true; //If both match, then the loop will return true.
}

binaryReader.Close();
return false; /*This will return false, as the username
and password entered do not exist in the file, or do not match any records.*/
}
public static int Records()
{
FileStream fileStream = File.OpenRead("credentials.bin");
//Counts how many records are in the binary file (how many users are registered).
BinaryReader binaryReader = new BinaryReader(fileStream);
//This method can be used in the score table to calculate the size of scoreArray
int records = 0;

while (binaryReader.PeekChar() != -1)
{
string name = binaryReader.ReadString();
string surname = binaryReader.ReadString();
string email = binaryReader.ReadString();
string username = binaryReader.ReadString();
string password = binaryReader.ReadString();
records++;
}
binaryReader.Close();
return records;

}
public static int Records()
{
FileStream fileStream = File.OpenRead("credentials.bin");
//Counts how many records are in the binary file (how many users are registered).
BinaryReader binaryReader = new BinaryReader(fileStream);
//This method can be used in the score table to calculate the size of scoreArray
int records = 0;

while (binaryReader.PeekChar() != -1)
{
string name = binaryReader.ReadString();
string surname = binaryReader.ReadString();
string email = binaryReader.ReadString();
string username = binaryReader.ReadString();
string password = binaryReader.ReadString();
records++;
}
binaryReader.Close();
return records;

}
thats then the reading in a different class
phaseshift
phaseshift•2y ago
Right. So you have debugged this, and looked at the values, correct?
surname, username, password and user ID number*/
userArray[x, 0] = binaryReader.ReadString()
surname, username, password and user ID number*/
userArray[x, 0] = binaryReader.ReadString()
cgxlm
cgxlmOP•2y ago
Ahh no
phaseshift
phaseshift•2y ago
$debug
MODiX
MODiX•2y ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
cgxlm
cgxlmOP•2y ago
im sorry i just realised the errror there now omg i spent like the past 2 hours looking thrugh this it was if (userArray[x, 2] == username) if (userArray[x, 3] == password) this part but i had my username as the third and the password should be the fourth
phaseshift
phaseshift•2y ago
different order? yeah
cgxlm
cgxlmOP•2y ago
its always something so simple lo;
Pobiega
Pobiega•2y ago
off by one errors can take hours to debug 😄
cgxlm
cgxlmOP•2y ago
yeah ill always have like tiny spelling errors that will mess everything up lol
Want results from more Discord servers?
Add your server