简体   繁体   English

BinaryWriter.Write(Int32)

[英]BinaryWriter.Write(Int32)

I have a file that containt struct like these : 我有一个包含这样的结构的文件:

public struct index
{
    public string word;         //30 bytes
    public int pos;             //4 bytes
};

For the word, I make sure I extend it to 30 bytes before writing it and the pos I write it as it is because I know that an int32 is 4 bytes. 对于这个词,我确保在将其写入之前将其扩展到30个字节,然后按原样写pos,因为我知道int32是4个字节。

Here is the code to write in the file : 这是要写入文件的代码:

for (i = 0; i < ind.word.Length; i++)
{
   bword[i] = (byte)idx_array[l].word[i];
}
for (; i < SIZE_WORD; i++) //30
{
   bword[i] = 0;
}
bw_idx.Write(bword, 0, SIZE_WORD);
bw_idx.Write(ind.pos);

The code compile and works well except for one thing : the int32 dosen't get written. 该代码可以编译并可以正常工作,除了一件事:int32不会被编写。 If I check the file using notepad++ I see this where the int should be : SOH NULL NULL NULL I looked up SOH and it is supposed to be SOH (Start Of Heading): 如果我使用notepad ++检查文件,则会看到int应该位于的位置: SOH NULL NULL NULL我查找了SOH,应该是SOH(标题开始):

This character is used to indicate the start of heading, which may contain address or routing information. 此字符用于指示标题的开始,其中可能包含地址或路由信息。

Can any of you see why my int32 is not writing? 你们谁能看到我的int32不写的原因?

code for you to try (the file will be saved in the bin debug folder of the project) : 代码供您尝试(文件将保存在项目的bin调试文件夹中):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace test_write
{
    class Program
    {
        struct enreg
        {
            public string word;
            public int pos;
        }

        const int SIZE_WORD = 30; //30 bytes
        const int SIZE_POS = 4; //4 bytes

        static byte[] bword = new byte[SIZE_WORD];        
        static byte[] bpos = new byte[SIZE_POS];

        static void Main(string[] args)
        {
            enreg enr = new enreg();

            Console.Write("word : ");
            enr.word = Console.ReadLine();
            Console.Write("\anumber : ");
            enr.pos = int.Parse(Console.ReadLine());

            FileStream fs = File.Open("temp", FileMode.Create, FileAccess.ReadWrite);
            BinaryWriter bw = new BinaryWriter(fs);

            int i = 0;
            for (i = 0; i < enr.word.Length; i++)
            {
                bword[i] = (byte)enr.word[i];
            }
            for (; i < SIZE_WORD; i++)
            {
                bword[i] = 0;
            }
            bpos = BitConverter.GetBytes(enr.pos);

            bw.Write(bword, 0, SIZE_WORD);
            bw.Write(bpos, 0, SIZE_POS);

            fs.Close();

        }
    }
}

BinaryWriter writes its results encoded in a binary format. BinaryWriter二进制格式写入其结果。 If you want to output text, use a StreamWriter . 如果要输出文本,请使用StreamWriter Text and bytes are not directly related. 文本和字节不直接相关。 You cannot treat them as identical. 您不能将它们视为相同。

And please do not write chars by converting them to byte. 并且请不要通过将字符转换为字节来编写字符。 If you don't know why this is not possible, please read up about character encodings. 如果您不知道为什么不能这样做,请阅读有关字符编码的内容。 This is quite fundamental knowledge which every programmer needs to have. 这是每个程序员都需要具备的非常基础的知识。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM