C
C#•5mo ago
RuarasSwiftArrow

Trying to read from a binary file

New to c#, trying to validate a username and password from a binary file, but getting the issue 'Too many bytes in what should have been a 7-bit encoded integer. Not too familiar with binary readers and writers, can anyone help?
No description
No description
64 Replies
Pobiega
Pobiega•5mo ago
That some funky code there especially since you rewind the stream before trying to read, in a loop what is that loop and that rewind doing there?
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
I'm honestly not sure, haven't been taught how to do this yet and have little understanding of how it all works so this is cobbled together from the little I do know and various stack overflow questions 😭
Jimmacle
Jimmacle•5mo ago
is there a reason you're using a binary reader?
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
as part of the assignment we have to read and write from a binary file
Pobiega
Pobiega•5mo ago
alright so just walk us through exactly what you are trying to do here
Jimmacle
Jimmacle•5mo ago
yeah readstring expects the data to be in a specific format namely length-prefixed with 7 bit characters
Pobiega
Pobiega•5mo ago
How was this binary file made? BinaryWriter?
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
So in the binary file there are user details, username, password, then a few others. I want to take user input, which is fine, and then check it against the username and password stored in the binary file. The username written to the binary file was created by a binary writer writing a string, and the password by a binary writer writing an int (I hashed the password) oh yeah i forgot there are multiple users details in a single file so I need to loop through them all
Pobiega
Pobiega•5mo ago
can you show the write code? so we know the format of the reading
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
No description
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
player is a class, Username is string and Password is an int
Pobiega
Pobiega•5mo ago
Mhm... and you just... append to this bin file? no lead int that tells you how many players the file contains?
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
yeah, this was the code we were given to do this
Pobiega
Pobiega•5mo ago
okay okay, thats fine a little less safe, but still doable I'll begin with posting a little snippet of code
private static void Read()
{
var fs = File.OpenRead("data.bin");
using var br = new BinaryReader(fs);

// str, int, str
Console.WriteLine(br.ReadString());
Console.WriteLine(br.ReadInt32());
Console.WriteLine(br.ReadString());

br.Close();
}

private static void Write()
{
var fs = File.OpenWrite("data.bin");
using var bw = new BinaryWriter(fs);

// str, int, str
bw.Write("I am a test string");
bw.Write(512);
bw.Write("Second string");

bw.Close();
}
private static void Read()
{
var fs = File.OpenRead("data.bin");
using var br = new BinaryReader(fs);

// str, int, str
Console.WriteLine(br.ReadString());
Console.WriteLine(br.ReadInt32());
Console.WriteLine(br.ReadString());

br.Close();
}

private static void Write()
{
var fs = File.OpenWrite("data.bin");
using var bw = new BinaryWriter(fs);

// str, int, str
bw.Write("I am a test string");
bw.Write(512);
bw.Write("Second string");

bw.Close();
}
note how we must write and read in the exact same order, with the exact same types and the exact same count of things binary wont let you skip around in the file, you need to know exactly what is where so if you have an unknown amount of players, you'll need to read until the stream reaches its end, and you must read an entire full "player" struct at a time
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
right I think I'm understanding now, I use the read to iterate through what is in the file, and check against it, then move to the next line, and I need to read everything on a line before going to the next one
Pobiega
Pobiega•5mo ago
.. what?
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
maybe I'm not understanding give me a minute to see thank you so much though this is helping a lot
Pobiega
Pobiega•5mo ago
write will uh... write to the file :p you dont wanna write when reading.
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
I meant to type read sorry tired ahaha changed it now, am i on the right tracks?
Pobiega
Pobiega•5mo ago
something along those lines but there is no concept of "lines" in a binary file its just a stream of binary data BUT, since we know the structure of the data inside that stream, as long as we read one "block" at a time, we're good
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
I think i got it now, I'll probably be back here in 5 minutes to ask for me help however haha thank you
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
I'm still getting this original issue
No description
Pobiega
Pobiega•5mo ago
hm, your file must be malformed I'd recommend re-generating it
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
ok I'll give that a go thanks
Pobiega
Pobiega•5mo ago
ReadString reads a number from the file, which is how many chars to read for the string, then it reads that amount of chars. If that fails, you get this error it most likely means your binary file didnt start with a string, or was somehow modified after it was created binary is very hard to work with for this reason, and you cant just open the file in a text editor like with json etc
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
that sorted one issue and uncovered another
No description
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
ah i opened it a few times with notepad that must have triggered it
Pobiega
Pobiega•5mo ago
that might be it yeah hm, why would it try reading beyond the end of the stream... do you have any loops or stuff going here? also when you write, you dont write any ints you just write 5 strings
Pobiega
Pobiega•5mo ago
No description
Pobiega
Pobiega•5mo ago
at least thats what you showed us before
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
player.Password is an integer
Pobiega
Pobiega•5mo ago
ah okay
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
because I hash the password yeah I have a for loop going around it here one sec
Pobiega
Pobiega•5mo ago
show me da loop.
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
No description
Pobiega
Pobiega•5mo ago
uuh what
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
just one that should only run once to test
Pobiega
Pobiega•5mo ago
oh there are many things wrong here you need to open the stream and reader before the loop and close the reader AFTER the loop also, here is a more appropriate "end of line" check: while (br.BaseStream.Position < br.BaseStream.Length)
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
the "end of line" check is for the for loop yeah?
Pobiega
Pobiega•5mo ago
ye
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
yeah im still getting the same issue
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
No description
Pobiega
Pobiega•5mo ago
and you're sure users.bin is fine now?
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
havent touched it since
Pobiega
Pobiega•5mo ago
hm cause it all works fine on my end
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
remade it, made it by saving a blank file from notepad as a .bin in case that matters
Pobiega
Pobiega•5mo ago
oh no no delete the file don't ever open binary files with a text editor
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
how do i manually generate one then?
Pobiega
Pobiega•5mo ago
with your "write" program/method
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
yeah so deleted it and let it create through the program and error still occurs
Pobiega
Pobiega•5mo ago
hm might be time to install a hex editor and inspect your file
Jimmacle
Jimmacle•5mo ago
iirc vscode has one built in, if not there's an extension
Pobiega
Pobiega•5mo ago
not built in, but defo extensions I personally use ImHex
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
essentially turning it on and off again to see if it sorts itself it didnt, just to check, it always accesses the file withing the project right?
Pobiega
Pobiega•5mo ago
and you are sure you are writing 1 string 1 int32 3 more strings, per user? well, the file in your bin/net8.0/ folder if its generated and read at runtime
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
I'm using 5.0 for reference not by choice
Pobiega
Pobiega•5mo ago
thats fine but yeah, the file will be at YourProjectHere\bin\Debug\net5.0\Users.bin
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
yeah just searched for writers and thats all I could see wait i might be an idiot
Pobiega
Pobiega•5mo ago
these are words all programmers are very familiar with
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
damn I'm working on an older version where my username is stored as an int (I hashed it too for some reason), changed it recently but forgot to use that one 😭 let me run it now and see if it works
Pobiega
Pobiega•5mo ago
yeah that'd majorly fuck this up
RuarasSwiftArrow
RuarasSwiftArrow•5mo ago
it works! thank you so much, sorry for wasting your time at the end there
Pobiega
Pobiega•5mo ago
can highly recommend imhex btw. I taught it to recognize a C# string and then my "player" structure
Pobiega
Pobiega•5mo ago
No description