1

I'm trying to convert an old game of mine to windows 8 and I'm having a lot of trouble with my file loading. I'm trying a simple test with DataReader but I don't get the correct values.

First I write like this:

StorageFolder folder = ApplicationData.Current.LocalFolder;
        StorageFile file = await folder.CreateFileAsync("file.dat",CreationCollisionOption.ReplaceExisting);

        using (IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
        {
            using (IOutputStream outputStream = fileStream.GetOutputStreamAt(0))
            {
                using (DataWriter writer = new DataWriter(outputStream))
                {
                    try
                    {
                        writer.UnicodeEncoding = UnicodeEncoding.Utf8;
                        writer.ByteOrder = ByteOrder.LittleEndian;
                        writer.WriteInt32(1);
                        writer.WriteInt32(2);
                        await writer.StoreAsync();
                        writer.DetachStream();
                    }
                    catch (IOException)
                    {

                    }    
                }
            }
        }

Then I read

        StorageFolder folder = ApplicationData.Current.LocalFolder;
        StorageFile file = await folder.GetFileAsync("file.dat");

        using (var fileStream = await file.OpenReadAsync())
        {
            using (IInputStream inputStream = fileStream.GetInputStreamAt(0))
            {
                using (DataReader reader = new DataReader(inputStream))
                {
                    reader.UnicodeEncoding = UnicodeEncoding.Utf8;
                    reader.ByteOrder = ByteOrder.LittleEndian;
                    await reader.LoadAsync((uint)fileStream.Size);
                    var number = reader.ReadInt32();
                    var number2 = reader.ReadInt32();
                    reader.DetachStream();
                }
            }
        }

But I don't get 1 and 2 when I read, just two really big numbers. So, something I missed this is now how you do it? I'm also trying to figure out the best way to work with strings, am I suppose to also write the byte length now as it ask for a "codeUnitCount" when I read?

It just seems like everything is a step back from the old binary reader.

tshepang
  • 12,111
  • 21
  • 91
  • 136
MilleB
  • 1,470
  • 2
  • 19
  • 32
  • If the game already works on a previous version of WIndows, it should already work for Windows 8, unless you mean your trying to have it load as a Modern UI application. More information is required to help you. For example what data do you get, and what data are you suppose to get, and provide us an example of the data your loading. – Security Hound Oct 26 '12 at 09:19
  • Well I forgot to add that I'm doing it to an app in Marketplace so that's why I think I have to do it this way. But I'm just trying with the short example in my first post now and as I don't get it right I think I do something wrong. I just try to save two numbers (1 and 2) but when I load I get (1919252047) and (1819438967). – MilleB Oct 26 '12 at 09:25
  • Is the writing or reading wrong? Try writing zero zero and see if the files has 8 zero bytes. Then you know where to start looking . – IvoTops Oct 26 '12 at 11:17
  • I thinks it's the reading. I also tried with zero zero as you said and the file it create is 8 byte. – MilleB Oct 26 '12 at 11:20
  • I also checked the file when I saved with 1 and 2 and I got "01 00 00 00 02 00 00 00" so it seems correct. – MilleB Oct 26 '12 at 11:39

0 Answers0