0

I have the following code:

LZW lzw = new LZW();
int[] a = lzw.Encode(imageBytes);

FileStream fs = new FileStream("image-text-16.txt", FileMode.Append);
BinaryWriter w = new BinaryWriter(fs);

for (int i = 0; i < a.Length; i++)
{
   w.Write(a[i]);

}

w.Close();
fs.Close();

How to read array elements from the file? I tried several ways. For example, I wrote the length of the array to the file, and I tried to read the number. However, I had failed.

Note. I need to get the int array.

2
  • which LZW library are you using? Commented Feb 9, 2013 at 13:48
  • @LZW, this is my class pastebin.com/KX2veyxe Commented Feb 9, 2013 at 13:52

2 Answers 2

7

Are you looking for this:

var bytes = File.ReadAllBytes(@"yourpathtofile");

Or more something like:

        using (var filestream = File.Open(@"C:\apps\test.txt", FileMode.Open))
        using (var binaryStream = new BinaryReader(filestream))
        {
            for (var i = 0; i < arraysize; i++)
            {
                Console.WriteLine(binaryStream.ReadInt32());
            }
        }

Or, a small example with unit tests:

Create a binary file with integers...

    [Test]
    public void WriteToBinaryFile()
    {
        var ints = new[] {1, 2, 3, 4, 5, 6, 7};

        using (var filestream = File.Create(@"c:\apps\test.bin"))
        using (var binarystream = new BinaryWriter(filestream))
        {
            foreach (var i in ints)
            {
                binarystream.Write(i);
            }
        }
    }

And a small example test of reading from a binary file

    [Test]
    public void ReadFromBinaryFile()
    {
        // Approach one
        using (var filestream = File.Open(@"C:\apps\test.bin", FileMode.Open))
        using (var binaryStream = new BinaryReader(filestream))
        {
            var pos = 0;
            var length = (int)binaryStream.BaseStream.Length;
            while (pos < length)
            {
                var integerFromFile = binaryStream.ReadInt32();
                pos += sizeof(int);
            }
        }
    }

Another approach of reading from binary file

    [Test]
    public void ReadFromBinaryFile2()
    {
        // Approach two
        using (var filestream = File.Open(@"C:\apps\test.bin", FileMode.Open))
        using (var binaryStream = new BinaryReader(filestream))
        {
            while (binaryStream.PeekChar() != -1)
            {
                var integerFromFile = binaryStream.ReadInt32();
            }
        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

+1, didn't catch, that he is dealing with binary files (due to txt extension and int in the question header)
@bas, I have a problem with the definition of the size of the array (arraysize).
@Denis, I'm watching a movie atm, hope the update helps out enough. Stefan also gave third option to iterate through the binary file fs.Length / sizeof(int)
1

I'd say the other way around. Only thing is you don't know the size before reading it, so calculate that first. Oh and I'd use 'using' to make sure things are disposed (and closed) properly:

        int[] ll;
        using (FileStream fs = File.OpenRead("image-text-16.txt"))
        {
            int numberEntries = fs.Length / sizeof(int);
            using (BinaryReader br = new BinaryReader(fs))
            {
                ll = new int[numberEntries];
                for (int i = 0; i < numberEntries; ++i)
                {
                    ll[i] = br.ReadInt32();
                }
            }
        }
        // ll is the result

The thing I don't really understand is why you're writing an int[] from LZW, but I guess there's a reason for that...

1 Comment

He is writing "int[] from LZW" because of when u declare ana array you must include the datatype of the array that means the members of the array will hold the data type of the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.