Question on writing / reading ValueTree binary files


#1

Hello. My (simple) write and read code looks like this:

void Resources::saveButtonPressed()
{
FileChooser fC (“Choose a file to save…”, File::getSpecialLocation (File::userHomeDirectory), “*”, true);

if (fC.browseForFileToSave (true))
{
    FileOutputStream* fOS = fC.getResult().withFileExtension (".isf").createOutputStream();
    mainVT.writeToStream (*fOS); 
    fOS->flush();
    fOS = nullptr;
}

}

void Resources::loadButtonPressed()
{
FileChooser fC (“Select File…”, File::getSpecialLocation (File::userHomeDirectory), “*.isf”, true);

if (fC.browseForFileToOpen())
{
    FileInputStream* fIS = fC.getResult().createInputStream();
    mainVT.readFromStream (*fIS);
    fIS = nullptr;
}

}

This writes a file, but when I’ve read the tree, it doesn’t come up with the saved values that go along with the properties. The file looks like this opened in TextEditor:

mainVTtransportInterfaceVTet0BPMPerc Y@t0BPMFreeSet ^@t1BPMPerc Y@t1BPMFreeSet ^@t2BPMPerc Y@t2BPMFreeSet ^@t3BPMPerc Y@t3BPMFreeSet ^@t4BPMPerc Y@t4BPMFreeSet ^@t5BPMPerc Y@t5BPMFreeSet ^@t6BPMPerc Y@t6BPMFreeSet ^@t7BPMPerc Y@t7BPMFreeSet ^@t8BPMPerc Y@t8BPMFreeSet ^@t0Modet1Modet2Modet3Modet4Modet5Modet6Modet7Modet8Mode

So it appears that the properties have been saved, but the var values aren’t being loaded. Can anyone help? Thank you


#2

Maybe start by learning about managing object lifetimes in c++ - the code you posted just leaks all your stream objects, so if all your code is written that way there could be any number of other errors in there.


#3

This was really just code to start testing it out, I presumed by setting the stream to nullptr after using it, the lifetime would be ended. I’m not used to using stream objects at all.


#4

Setting the streams to scoped pointers doesnt seem to help.

void Resources::saveButtonPressed()
{
FileChooser fC (“Choose a file to save…”, File::getSpecialLocation (File::userHomeDirectory), “*”, true);

if (fC.browseForFileToSave (true))
{
    ScopedPointer <FileOutputStream> fOS = fC.getResult().withFileExtension (".isf").createOutputStream();
    mainVT.writeToStream (*fOS); 
    fOS->flush();
    fOS.release();
}

}

void Resources::loadButtonPressed()
{
FileChooser fC (“Select File…”, File::getSpecialLocation (File::userHomeDirectory), “*.isf”, true);

if (fC.browseForFileToOpen())
{
    ScopedPointer <FileInputStream> fIS = fC.getResult().createInputStream();
    mainVT.readFromStream (*fIS);
    fIS.release();
}

}


#5

readFromStream is a static function, which returns the new ValueTree

something like:

ValueTree newValueTree (ValueTree::readFromStream(*fIS));
if (newValueTree.isValid())
{
   mainVT=newValueTree;
};

#6

Ah that sounds like it makes sense. Sadly it hasn’t fixed the issue, but I have altered my code to reflect this:

void Resources::saveButtonPressed()
{
FileChooser fC (“Choose a file to save…”, File::getSpecialLocation (File::userHomeDirectory), “*”, true);

if (fC.browseForFileToSave (true))
{
    ScopedPointer <FileOutputStream> fOS = fC.getResult().withFileExtension (".isf").createOutputStream();
    mainVT.writeToStream (*fOS); 
    fOS->flush();
    fOS.release();
}

}

void Resources::loadButtonPressed()
{
FileChooser fC (“Select File…”, File::getSpecialLocation (File::userHomeDirectory), “*.isf”, true);

if (fC.browseForFileToOpen())
{
    ScopedPointer <FileInputStream> fIS = fC.getResult().createInputStream();
    ValueTree newValueTree (ValueTree::readFromStream (*fIS));
    
    if (newValueTree.isValid()) mainVT = newValueTree;
    else std::cout << "Not Valid" << std::endl;

    fIS.release();
}

}


#7

By the look of the Text File, it looks like the property names are written, but not the values. Is this presumption correct? If so, how is the save not working?


#8

its a binary file, if the properties are numbers, you not see them in a texteditor as decimal numbers,


#9

Fair enough. I’ll keep on digging on the problem then. It makes it tricky when I’m not sure whether it’s the write or read that’s not working!


#10

This is actually the opposite of what you want:

ScopedPointer::release():
Removes the current object from this ScopedPointer without deleting it.
This will return the current object, and set the ScopedPointer to a null pointer.

You either do nothing, then the opject is deleted when the ScopedPointer object goes out of scope (hence the name), or you call fIs = nullptr; to enforce deletion at a specific point inside of the scope.

To your actual problem: does “isValid()” evaluate to true or not?
ValueTree::getProperty() returns an object of type var, so look there how to use this values…

Good luck