# File read failing

**URL:** <https://forum.juce.com/t/file-read-failing/23265>\
**Category:** General JUCE discussion\
**Created:** [July 10, 2017, 2:21am UTC](https://forum.juce.com/t/file-read-failing/23265 "2017-07-10T02:21:40Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriswratt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chriswratt/32/2414_2.png) [@chriswratt](https://forum.juce.com/u/chriswratt)\
**Post date:** [July 10, 2017, 2:21am UTC](https://forum.juce.com/t/file-read-failing/23265/1 "2017-07-10T02:21:40Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [July 10, 2017, 2:45am UTC](https://forum.juce.com/t/file-read-failing/23265/2 "2017-07-10T02:45:45Z")

</div>

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

Rail

---

<div class="post-metadata">

**Author:** ![chriswratt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chriswratt/32/2414_2.png) [@chriswratt](https://forum.juce.com/u/chriswratt)\
**Post date:** [July 10, 2017, 3:02am UTC](https://forum.juce.com/t/file-read-failing/23265/3 "2017-07-10T03:02:45Z")

</div>

Thanks!!!

---

<div class="post-metadata">

**Author:** ![chriswratt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chriswratt/32/2414_2.png) [@chriswratt](https://forum.juce.com/u/chriswratt)\
**Post date:** [July 10, 2017, 3:13am UTC](https://forum.juce.com/t/file-read-failing/23265/4 "2017-07-10T03:13:57Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![mcmartin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/mcmartin/32/7432_2.png) [@mcmartin](https://forum.juce.com/u/mcmartin)\
**Post date:** [July 10, 2017, 4:56am UTC](https://forum.juce.com/t/file-read-failing/23265/5 "2017-07-10T04:56:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chriswratt](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chriswratt/32/2414_2.png) [@chriswratt](https://forum.juce.com/u/chriswratt)\
**Post date:** [July 10, 2017, 5:10am UTC](https://forum.juce.com/t/file-read-failing/23265/6 "2017-07-10T05:10:05Z")

</div>

perfect, that’s what I was looking for 🙂

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [July 10, 2017, 7:53am UTC](https://forum.juce.com/t/file-read-failing/23265/7 "2017-07-10T07:53:40Z")

</div>

Best to not use heap allocation at all:

```
FileInputStream f (someFile);

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

```
