- Home /
Reading and writing binary file, file unexpectedly large?
This is a continuation of my last question.
I'm trying to write to a binary file in the editor, then read from the file at runtime. The method I'm using works, but it's creating a 16mb file, while I was expecting about a 2mb file. What am I doing wrong? This obviously isn't writing just 0's and 1's for the bool values, but char bytes instead. And I thought this stepped bit by bit through the file or did I misunderstand this page msdn.microsoft.com
// in editor this is called to create the file
private void WriteBinaryFile()
{
string filePath = @"C:\PathToProject\Assets\Resources\";
using(BinaryWriter writer = new BinaryWriter(File.Open(filePath + "file.txt", FileMode.Create)))
{
for(int i = 0; i < 16777215; i++)
{
writer.Write(largeBoolArray[i]);
}
writer.Close();
}
}
// at runtime this is called to read the file
private void ReadBinaryFile()
{
if(File.Exists("Assets/Resources/file.txt"))
{
using(BinaryReader reader = new BinaryReader(File.Open("Assets/Resources/file.txt", FileMode.Open)))
{
for(int i = 0; i < 16777215; i++)
{
largeBoolArray[i] = reader.ReadBoolean();
}
reader.Close();
}
}
else
Debug.LogError("Binary file does not exist in ReadBinaryFile()!");
}
Answer by maccabbe · Apr 14, 2015 at 07:31 PM
The smallest addressable size is 1 byte so when you save a bool it takes up all the space of the byte. To make your file smaller you should pack multiple bools into a type that takes up at least one byte using bitwise operators. Then when you load the array you should similarly unpack the data. Here is something to get you started.
private void WriteBinaryFile(string filePath) {
using(BinaryWriter writer=new BinaryWriter(File.Open(filePath+"file.txt", FileMode.Create))) {
int toSave=0;
int x=0;
for(int i=0; i<16777215; i++) {
if(largeBoolArray[i]) {
toSave=toSave|(1<<x);
}
x++;
if(32<=x) {
writer.Write(toSave);
x=0;
toSave=0;
}
}
writer.Close();
}
}
private void ReadBinaryFile() {
if(File.Exists("Assets/Resources/file.txt")) {
using(BinaryReader reader=new BinaryReader(File.Open("Assets/Resources/file.txt", FileMode.Open))) {
int toLoad;
for(int i=0; i<16777215;) {
toLoad=reader.ReadInt32();
for(int x=0; x<32 && i<16777215; ){
largeBoolArray[i]=((toLoad>>x)&1)==1;
x++;
i++;
}
}
reader.Close();
}
}
else
Debug.LogError("Binary file does not exist in ReadBinaryFile()!");
}
Ok cool, I appreciate the answer, I'll report back later if I'm able to get it working for me.
@$$anonymous$$accabbe
When I test it I get true for every value in the array when reading it back. I've copied it almost exactly as you had it, and don't know anything about bitwise operations, so I'm not sure whats wrong. I tried changing ReadInt32 to ReadByte and that produces wrong results - and I'm taking shots in the dark here lol. Any idea why it's getting true for every value?
Didn't read it closely or test.. but I think the Read function needs a check for >=32 like the writer has... otherwise (toLoad>>x) is going to "shift the heck" out of those values... beyond 32 bits.
Hmmm wish I knew what is going on, I'll try and set up a similar loop in the reader and... guess and check lol
@shopguy what the purpose of the line with "for (int x = 0; x >= 0 && i < 16777215; )" ? Any idea if that's part of the problem?
Running a count through these loops I see that it totals the right amount of 16777215 values (inside the for int x... loop), so it is setting up enough to fill the array... hmmm...
Oh yeah, that line was originally int x=31; x>=0
but I felt like it should be x=0; x<32
but forgot to change the x>=0
Your answer
![](https://koobas.hobune.stream/wayback/20220613193124im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with my binary files 1 Answer
'Too many bytes in what should have been a 7 bit encoded Int32' and idk what I'm doing wrong... 1 Answer
[Serialization] Multiple classes into one file? 1 Answer
Compiling Universal App in windows which won't run on my Mac 2 Answers
Unity Asset Store and Text Serialisation 0 Answers