File read/random access


  • It seems that standard C library fopen etc.. and also CreateFile2 doesn't work for ex. files from video library even if user choose it with File picker.
    I'm try to port existing C++ library, but it's using standard C library.
    So I'll have to write some wrapper, but I'm getting nowhere.
    I checked all available samples but still can't manage to do this. I can't belive how MS managed to complicate such simple thing.
    What is replacemnt for simple  _read( fh, buffer, nbytes ) and _lseek( fh, 0L, SEEK_SET) ?
    I just need to read data from different position in file to memory buffer. How can I do that ? Could some one please provide some sample/link anything ?


    Sunday, January 13, 2013 1:51 AM

All replies

  • Your app doesn't have file system access outside of its application data and install directory, so you can't use file system API elsewhere. Permissions granted through the FilePicker require using the StorageFile returned from the picker. StorageFile.OpenRead returns a random access stream onto the file contents.

    See Skip the path: stick to the StorageFile for more commentary on paths vs. StorageItems.


    Sunday, January 13, 2013 9:58 AM
  • Yes, I understand this, I don't like it, but ok.
    So I use StorageFile.OpenAsync and I get IRandomAccessStream. Now what ?
    File access sample then creates DataReader and call DataReader->LoadAsync, but I don't think I want that.
    If I understand correctly this loads whole file to memory ? I can't do this, because fle is too large.
    There is also DataReader.ReadBuffer, but how can I seek in stream ?
    There is GetInputStreamAt method that returns IInputStream.
    Do I have to call GetInputStreamAt every time I want to read from different position ?
    Also if I call IInputStream.ReadAsync, IBuffer is returned and the only methods are Capacity and Length.
    How can I access data in buffer ?

    OK, didn't noticed before, returned IRandomAccessStream from StorageFile.OpenAsync has actually all methods of FileRandomAccessStream class.
    There is Seek method. Also I found some more info about IBuffer access. Still don't like this async methods.
    I'm doing reading in separate thread anyway, it just unneeded overhead.

    • Edited by Dereck S Sunday, January 13, 2013 12:06 PM
    Sunday, January 13, 2013 11:58 AM
  • I still can't get this work.

    How should FileRandomAccessStream.ReadAsync be used ?

    This code doesn't compile:

    int size = 5000;
    IBuffer^ buffer = ref new Buffer(size);
    create_task(m_RandomAccessStream->ReadAsync(buffer,size,InputStreamOptions::None)).then([this](task<Windows::Foundation::IAsyncOperationWithProgress<IBuffer^, UINT32> > task)

    Error message is:

    error C2338: incorrect parameter type for the callable object in 'then'; consider _ExpectedParameterType or task<_ExpectedParameterType>

    Monday, January 14, 2013 1:04 AM