如何获得随机文件的确切大小 [英] how to get exact size of Random File
问题描述
Hellow
i使用Class1.cs创建了一个二进制文件Adata.bin,它有4个
属性,每个属性都可以设置并返回32个字符的字符串大小
所以一条记录的大小是= sizeof(char)* 4 * 32 = 256; sizeof(char)= 2;
我的目标是使用等式查找记录数
Nrecord =文件大小/一个记录的大小
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
MessageBox.Show(MyRandom.SIZE.ToString());// shows 256
,我做了N = 1,N = 2,N = 3
记得我机器中的sizeof(char)是2
跟随结果是由MessageBox(...... <当N = 1时,
大小由文件lenght = 132但是(sizeof(char))*(4属性)*(每个属性32个字符)= 128
当N = 2
大小由文件lenght = 264 ...................(8属性)....当N = 3
$时,............................... = 256
b $ b size by file lenght = 396 ...................(12 property).................. ................. = 384
似乎机器每个属性增加1个字符或字节。为什么呢没有添加si zeof(char)每个属性?!
如果使用64位机器中的代码会产生相同的结果吗?
需要说明请
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
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());
}
推荐答案
简单:查看BinaryWrite.Write的字符串参数定义: http://msdn.microsoft.com/en-us/library/yzxa6408(v = vs.110 ).aspx [ ^ ]
并阅读备注下的位。
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".
这篇关于如何获得随机文件的确切大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!