1

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.

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

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?

code for you to try (the file will be saved in the bin debug folder of the project) :

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();

        }
    }
}
ESD
  • 675
  • 2
  • 12
  • 35
  • I guess you'd better use a hex editor instead of Notepad++. [Frhed](http://frhed.sourceforge.net/en/) is a nice opensource one. Also check whether you have closed (or flushed) the file properly. – Alvin Wong Dec 08 '12 at 16:00
  • Also, I don't see `FileStream.Write(Int32)`. Do you mean [`BinaryWriter.Write(Int32)`](http://msdn.microsoft.com/en-us/library/24e33k1w(v=vs.100).aspx)? – Alvin Wong Dec 08 '12 at 16:01
  • it is well closed and even if I had flush it dosen't work – ESD Dec 08 '12 at 16:15
  • Perhaps you should isolate the problem and provide a short, self-contained code for us to reproduce the problem – Alvin Wong Dec 08 '12 at 16:17
  • Not sure I understand. The int *was* written. It resulted in the 4 bytes SOH, 0, 0, 0. Why do you think it was not written? Btw, the way you write the chars is not unicode-ready. – usr Dec 08 '12 at 16:18
  • well i'm expecting to see number 1 in binary – ESD Dec 08 '12 at 16:35
  • 1
    As I say, **use a hex editor** instead of Notepad++. – Alvin Wong Dec 08 '12 at 16:37
  • Frhed is even worst than notepad++ since it's only showing dots where notepad was showing SOH – ESD Dec 08 '12 at 16:52
  • You fundamentally misunderstand the difference between binary data and text. You'll have a lot more trouble completing this task as long as that's the case, you cannot write the string with BinaryWriter. You'll need to educate yourself, Petzold's book "Code" is a good one. – Hans Passant Dec 08 '12 at 17:27
  • I can write the string whit binary write and it works like a charm. + it's a way for me to know when the string end in the file by defining a default amount of bytes. I agree thats it's not the best way to do it but for now i'm following my teacher's code. Why do I textually see the word in the text file but not the number? – ESD Dec 08 '12 at 17:29

1 Answers1

1

BinaryWriter writes its results encoded in a binary format. If you want to output text, use a 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.

usr
  • 168,620
  • 35
  • 240
  • 369