Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MD_Reptile · Apr 14, 2015 at 05:35 PM · binaryfilesizebinarywriter

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()!");
     }


Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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()!");
 }

Comment
Add comment · Show 7 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MD_Reptile · Apr 14, 2015 at 07:58 PM 0
Share

Ok cool, I appreciate the answer, I'll report back later if I'm able to get it working for me.

avatar image MD_Reptile · Apr 14, 2015 at 11:11 PM 0
Share

@$$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?

avatar image shopguy · Apr 15, 2015 at 01:32 AM 0
Share

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.

avatar image MD_Reptile · Apr 15, 2015 at 01:44 AM 0
Share

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...

avatar image maccabbe · Apr 15, 2015 at 03:05 AM 1
Share

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

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges