Read data from a text file


Hello all,

I have the frequency response data of a filter stored in a text file which I am trying to get my plugin to read and store in arrays upon loading.

In the plugin processor’s constructor I have the following code:

FILE *file1;
file1 = fopen("TF1.txt", "r");

for(int i = 0; i < 2048; i++)
    fscanf(file1, "%f", &realNumber[i]);
    fscanf(file1, "%s", &imaginarySign[i]);
    fscanf(file1, "%f", &unsignedImaginaryNumber[i]);
    fscanf(file1, "%s", &imaginarySymbol[i]);

This code works absolutely fine in a simple command-line version of my filter plugin, however after I (successfully) build my juce project in xCode, the plugin crashes when trying to load a new instance of it in Reaper or Cubase. It is the ‘fscanf’ lines that are causing the plugin to crash.

At first I thought I hadn’t saved the text file in the right location but after copying and pasting it in every possible location in my juce project folder and the xcode derived data folder I am guessing I am missing something.



Maybe put at least a check

if (file1 != nullptr) {

after the fopen, if it was opened successfully?


@daniel I’ve just put the FOR loop in that IF statement and the plugin does not crash, so I guess it can’t locate the text file.

Any idea where it needs to be saved? or can I include it in my project using the projucer somehow?


Look at juces File class, that matkatmusic linked.
I suggest you use the SpecialLocations to construct the path for fopen, or better use the File class for reading and parsing as well.

To have a file inside the project, add it in the Projucer and you will find the contents as Char* in BinaryData like
String contents = String::fromUTF8 (BinaryData::filename_txt, BinaryData::filename_txtsize);
or similar…