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][1]
// 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*);*
-
}*
-
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 = reader.ReadBoolean();_
* }*
* reader.Close();*
* }*
* }*
* else*
* Debug.LogError(“Binary file does not exist in ReadBinaryFile()!”);*
* }*
_*[1]: Microsoft Learn: Build skills that open doors in your career