File read failing


#1

Hi y’all- I know that this is a super simple problem but I’ve been stuck on it for days and my google searches aren’t helping.
I am currently trying to read a midi file from disk on windows but the read always fails, here’s a boiled down version of my code:

File file(“d:\PluginDev\JuceDev\MidiSystem\Data\cMajorC3Ascending.mid”);
FileInputStream* inputStream = file.createInputStream();
MidiFile midiFile;
midiFile.readFrom(*inputStream); //fails here

It throws “sourceStream was nullptr”. Also if I check against file.exists() before trying to stream it in file doesn’t exist.
The file I’m trying to read is in my juce project and I took the absolute path from the visual studio properties of the midi file to be sure, I’ve also tried a whole bunch of different locations so I don’t think it’s just me screwing up the path…
Have I made some super obvious error?


#2

You have to escape your slashes or use File::getChildFile to build a properly formatted path.

Rail


#3

Thanks!!!


#4

Also is there a correct RAII way to deal with FileInputStream* inputStream = file.createInputStream(); without later calling delete on the FileInputStream? (I’m getting a memory leak whenever I close the plugin without using a delete).


#5

You should use a ScopedPointer<FileInputStream>
(see https://www.juce.com/doc/classScopedPointer for more details).


#6

perfect, that’s what I was looking for :slight_smile:


#7

Best to not use heap allocation at all:

FileInputStream f (someFile);

if (f.openedOK())
{
    ...etc