Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hellow
i created a binary file "Adata.bin" using Class1.cs which has 4
property, each property can set and return a string size of 32 characters
so the size of one record is =sizeof(char) * 4* 32 = 256; sizeof(char)=2;
my goal is to find number of record using the equation

Nrecord=size of file/size of one record

C#
MessageBox.Show(MyRandom.SIZE.ToString());// shows 256

in code below ,i made N=1,N=2,N=3
remember sizeof(char) in my machine is 2
the followin result were by MessageBox(......
when N=1
size by file lenght =132 but (sizeof(char))*(4 property) * (32 character per one property )=128
when N=2
size by file lenght =264 ...................(8 property)...................................=256
when N=3
size by file lenght =396 ...................(12 property)...................................=384

it seems the machine adds 1 char or byte per one property.why it did not adds sizeof(char) per property ?!
what if used the code in 64 bit machine would the result be same ?
needs explanation please
C#
private void FormLoad(object sender, EventArgs e)
        {   int N,i; string ss; long s1;
            //public static Class1 Tot = new Class1();
   MessageBox.Show("Class1.SIZE=" + Class1.SIZE.ToString());
   ss = "012345678901234567890123456789012";//<---32 characters
   Tot.BANDNAME = ss;  Tot.BANDNO   = ss;
   Tot.NO = ss;   Tot.COST = ss;
   fsMainInOut1=new FileStream("Adata.bin",FileMode.Truncate,FileAccess.ReadWrite);
   BinaryMainOut1 = new BinaryWriter(fsMainInOut1);
            fsMainInOut1.Seek(0 * Class1.SIZE, SeekOrigin.Begin);
            N = 3;//N=2;N=3;
            for (i = 0; i < N; i++)
            {
                BinaryMainOut1.Write(Tot.BANDNAME);
                BinaryMainOut1.Write(Tot.BANDNO);
                BinaryMainOut1.Write(Tot.NO);
                BinaryMainOut1.Write(Tot.COST);
            }
            BinaryMainOut1.Close(); fsMainInOut1.Close();
            FileInfo f = new FileInfo(FileName);
            s1 = f.Length;
            MessageBox.Show("size by file lenght =" + s1.ToString());
        }
Posted
Updated 22-Jul-14 9:16am
v3
Comments
PIEBALDconsult 22-Jul-14 15:20pm    
Is it really one byte per property? Have you tried writing only one property? Have you examined the contents of the file?

1 solution

Simple: look at the definition of BinaryWrite.Write for a string parameter: http://msdn.microsoft.com/en-us/library/yzxa6408(v=vs.110).aspx[^]
And read the bit under "Remarks".
 
Share this answer
 
Comments
Matt T Heffron 22-Jul-14 16:52pm    
+5...
Although ... neither MSDN nor you noted that the encoded length is a variable number of bytes.
For the OP, the length will be encoded in 1 byte.
However, in general, it will be 1 byte for every 7 bits required to represent the length.
Length 0-7F -- 1 byte
Length 80-3FFF -- 2 bytes
Length 4000-1FFFFF -- 3 bytes
etc.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900