locked
Reading binary data from a file

    Question

  • I would like to read binary data from a file.  Based on the documentation, the code should look something like this:

        create_task(item->folder->GetFileAsync("waveform.dat")).then([this](task<StorageFile^> t)
        {
            try {
                StorageFile^ file = t.get();
                create_task(FileIO::ReadBufferAsync(file)).then([this, file](task<IBuffer^> tt)
                {
                    try
                    {
                        IBuffer^ buffer = tt.get();
                        DataReader^ dataReader = DataReader::FromBuffer(buffer);
                        WriteOnlyArray<unsigned char, 1U>^ value = ref new WriteOnlyArray(unsigned char, MAX_RECORDING_SAMPLES);
                        dataReader->ReadBytes(value);
                        memcpy(m_samples, value->begin(), value->end()-value->begin());
                        m_waveSaved = true;
                        String^ fileContent = dataReader->ReadString(buffer->Length);
                        OutputTextBlock->Text = "The following " + buffer->Length.ToString() + " bytes of text were read from '" + file->Name + "':\n\n" + fileContent;
                    }
                    catch(COMException^ ex)
                    {
                       // rootPage->HandleFileNotFoundException(ex);
                    }
                });
            } catch(COMException^ ex) {
            }

    I'm getting a number of compiler errors related to WriteOnlyArray.  I have not been able to find documentation with a simple example showing the correct syntax.  Can someone post something?


    - Ken

    Thursday, September 20, 2012 12:53 PM

Answers

  • Hi,

    The constructor function of WriteOnlyArray is project private. Therefore, you can not instance a writeonlyarry in your codes.

    You can Right click WriteOnlyArray and Go To Definition to check this.

    We should use array to instead of it.

    	create_task(KnownFolders::DocumentsLibrary->GetFileAsync("testfile.txt")).then([this](task<StorageFile^> t)
         {
             try {
                 StorageFile^ file = t.get();
                 create_task(FileIO::ReadBufferAsync(file)).then([this, file](task<IBuffer^> tt)
                 {
                     try
                     {
                         IBuffer^ buffer = tt.get();
                         DataReader^ dataReader = DataReader::FromBuffer(buffer);
    					
    					 Array<unsigned char>^ value= ref new Array<unsigned char>(buffer->Length);
                         dataReader->ReadBytes(value);
                        // memcpy(m_samples, value->begin(), value->end()-value->begin());
                       //  m_waveSaved = true;
                        // String^ fileContent = dataReader->ReadString(buffer->Length);
                       //  OutputTextBlock->Text = "The following " + buffer->Length.ToString() + " bytes of text were read from '" + file->Name + "':\n\n" + fileContent;
                     }
                     catch(COMException^ ex)
                     {
                        // rootPage->HandleFileNotFoundException(ex);
                     }
                 });
             } catch(COMException^ ex) {
             }
    	});

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Wednesday, September 26, 2012 6:46 AM
    Friday, September 21, 2012 5:42 AM

All replies

  • Hi,

    The constructor function of WriteOnlyArray is project private. Therefore, you can not instance a writeonlyarry in your codes.

    You can Right click WriteOnlyArray and Go To Definition to check this.

    We should use array to instead of it.

    	create_task(KnownFolders::DocumentsLibrary->GetFileAsync("testfile.txt")).then([this](task<StorageFile^> t)
         {
             try {
                 StorageFile^ file = t.get();
                 create_task(FileIO::ReadBufferAsync(file)).then([this, file](task<IBuffer^> tt)
                 {
                     try
                     {
                         IBuffer^ buffer = tt.get();
                         DataReader^ dataReader = DataReader::FromBuffer(buffer);
    					
    					 Array<unsigned char>^ value= ref new Array<unsigned char>(buffer->Length);
                         dataReader->ReadBytes(value);
                        // memcpy(m_samples, value->begin(), value->end()-value->begin());
                       //  m_waveSaved = true;
                        // String^ fileContent = dataReader->ReadString(buffer->Length);
                       //  OutputTextBlock->Text = "The following " + buffer->Length.ToString() + " bytes of text were read from '" + file->Name + "':\n\n" + fileContent;
                     }
                     catch(COMException^ ex)
                     {
                        // rootPage->HandleFileNotFoundException(ex);
                     }
                 });
             } catch(COMException^ ex) {
             }
    	});

    Best regards,
    Jesse


    Jesse Jiang [MSFT]
    MSDN Community Support | Feedback to us

    • Marked as answer by Jesse Jiang Wednesday, September 26, 2012 6:46 AM
    Friday, September 21, 2012 5:42 AM
  • Hi Jesse,

    I am running into a situation with the code block above.

    I have the following code on my app,

    String^ errBuf = L"Success";

    create_task(KnownFolders::DocumentsLibrary->GetFileAsync("testfile.txt")).then([this](task<StorageFile^> t) { try {

    errBuf = "Try 1"; StorageFile^ file = t.get();

    create_task(FileIO::ReadBufferAsync(file)).then([this, file](task<IBuffer^> tt) { try {

    errBuf = "Try 2"; IBuffer^ buffer = tt.get(); DataReader^ dataReader = DataReader::FromBuffer(buffer); Array<unsigned char>^ value= ref new Array<unsigned char>(buffer->Length); dataReader->ReadBytes(value); // memcpy to my local data buffer errBuf = L"Done"; } catch(COMException^ ex) { errBuf = L"catch 2"; } }); } catch(COMException^ ex) {

    errBuf = L"catch 1"; } });

    now I expect, at the end of these tasks, errBuf to equal "Done"
    However, errBuf stays at "Try 1" and the local buffer does not contain any data (expected after the memcpy).

    What could be wrong?  I can read the file and get the size etc.  How can I debug into the each task?  

    Monday, December 3, 2012 9:22 PM