locked
How to add content to an access file? RRS feed

  • Question

  • Hi,

    According to the Metro style app samples "ApplicationData sample" and "File access sample" I'v know how to create a file and how to write content to the file.

    But I can't find how to add content to the end of a file  in samples as well as in library.

    I need to add some contents to the end of a file,just like the usage in c++:

             ofstream file("example.bin", ios::out | ios::app);

    How to do that in metro apps?

     

    Here is the code what I referenced:

     

    void MainPage::CreateFileButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        StorageFolder^ documentsFolder = KnownFolders::DocumentsLibrary;
        auto fileOperation = documentsFolder->CreateFileAsync("sample.txt", CreationCollisionOption::ReplaceExisting);
        fileOperation->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([this](IAsyncOperation<StorageFile^>^ operation) 
        {
            if (operation->Status == AsyncStatus::Completed)
            {
                StorageFile^ fileNew = operation->GetResults();
                Scenario1Output_textblock->Text = "The file " + fileNew->FileName + " was created.";
            }
            else
            {
                Scenario1Output_textblock->Text = "There was an error creating sample.txt.";
            }
        });
        fileOperation->Start();
    }
    
    void MainPage::WriteButton_Click(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
    {
        // First attempt to get the file
        StorageFolder^ documentsFolder = KnownFolders::DocumentsLibrary;
        auto fileOperation = documentsFolder->GetFileAsync("sample.txt");
        MainPage^ page = this;
        fileOperation->Completed = ref new AsyncOperationCompletedHandler<StorageFile^>([page](IAsyncOperation<StorageFile^>^ operation) 
        {
            if (operation->Status == AsyncStatus::Completed)
            {
                // Get the returned file and attempt to open it for a read+write operation
                StorageFile^ storagefileSample = operation->GetResults();
                auto openOperation = storagefileSample->OpenAsync(FileAccessMode::ReadWrite);
                openOperation->Completed = ref new AsyncOperationCompletedHandler<IRandomAccessStream^>([page](IAsyncOperation<IRandomAccessStream^>^ operation)
                {
                    if (operation->Status == AsyncStatus::Completed)
                    {
                        // Create a DataReader to read the opened file's contents
                        IRandomAccessStream^ accessStream = operation->GetResults();
                        IOutputStream^ outputStream = accessStream->GetOutputStreamAt(0);
                        DataWriter^ dataWriter  = ref new DataWriter(outputStream);
    
                        // Write the string and store it in the writer
                        dataWriter->WriteString(page->Scenario2Textbox->Text);
                        auto writeOperation = dataWriter->StoreAsync();
                        writeOperation->Completed = ref new AsyncOperationCompletedHandler<unsigned int>([page, outputStream](IAsyncOperation<unsigned int>^ operation)
                        {
                            auto flushOperation = outputStream->FlushAsync();
                            flushOperation->Completed = ref new AsyncOperationCompletedHandler<bool>([page](IAsyncOperation<bool>^ operation)
                            {
                                page->Scenario2Output_textblock->Text = "\"" + page->Scenario2Textbox->Text + "\" was written to sample.txt";
                            });
                            flushOperation->Start();
                        });
                        writeOperation->Start();
                    }
                });
                openOperation->Start();
            }
            else
            {
                page->Scenario3Output_textblock->Text = "The file sample.txt does not exist. Use scenario one to create this file.";
            }
        });
        fileOperation->Start();
    }
    

    Thanks

     


    Wednesday, February 1, 2012 11:14 AM

Answers

  • Hi Steven,

    You can seek to the end of the string before writing it. In your sample code you explicitly get the output stream from the beginning:

     IOutputStream^ outputStream = accessStream->GetOutputStreamAt(0);
                       
    


    If you want the end, look at the accessStream's size and call GetOutputStreamAt(accessSream->Size) to get the outputStream from the end.

    --Rob

     

    • Marked as answer by steven.steven Thursday, February 2, 2012 2:08 AM
    Wednesday, February 1, 2012 3:47 PM
    Moderator

All replies

  • Hi Steven,

    You can seek to the end of the string before writing it. In your sample code you explicitly get the output stream from the beginning:

     IOutputStream^ outputStream = accessStream->GetOutputStreamAt(0);
                       
    


    If you want the end, look at the accessStream's size and call GetOutputStreamAt(accessSream->Size) to get the outputStream from the end.

    --Rob

     

    • Marked as answer by steven.steven Thursday, February 2, 2012 2:08 AM
    Wednesday, February 1, 2012 3:47 PM
    Moderator
  • Thank you Rob,it Works.
    Thursday, February 2, 2012 2:08 AM