Forums > Dev

trouble using dictionary_read() with file from another directory

March 9, 2014 | 8:52 pm

Hi all,

I’m having trouble reading JSON files into a dictionary in my object. All works well when the file is in the same directory as my object, but if the file is anywhere outside the patch directory, dictionary_read() fails. Below is a skeletal example of my code. Anyone know what I’m doing wrong?

best,
Zachary

void read_json_file(t_smap *x, t_symbol *sourcefile)
{
    char filename[MAX_FILENAME_CHARS];
    short *path = NULL;
    path_frompathname(sourcefile->s_name, path, filename);
    
    t_dictionary *tempDict = dictionary_new();
    if (dictionary_read(filename, *path, &(tempDict)) == MAX_ERR_NONE) {
        // do stuff...
        post("JSON read succeeded.");
    }
    else {
        // do stuff...
        post("JSON read failed.");
    }
    
    // free temporary dictionary
    object_free(tempDict);
}

March 9, 2014 | 9:13 pm

Hi Zachary,

I haven’t tested your code but I think I know what you are doing wrong.
It doesn’t have anything to do with the dictionary_read() function, but with some misunderstanding of C pointers.

Try this:

void read_json_file(t_smap *x, t_symbol *sourcefile)
{
    char filename[MAX_FILENAME_CHARS];
    short path;
    path_frompathname(sourcefile->s_name, &path, filename);
    
    t_dictionary *tempDict = dictionary_new();
    if (dictionary_read(filename, path, &(tempDict)) == MAX_ERR_NONE) {
        // do stuff...
        post("JSON read succeeded.");
    }
    else {
        // do stuff...
        post("JSON read failed.");
    }
    
    // free temporary dictionary
    object_free(tempDict);
}

Hope this helps.

- Luigi


March 9, 2014 | 10:45 pm

Thanks again, Luigi. Now that was a dumb one. I think my brain is telling me to take a break ;).

best,
Zachary


Viewing 3 posts - 1 through 3 (of 3 total)