trouble using dictionary_read() with file from another directory


    Mar 10 2014 | 3:52 am
    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);
    }

    • Mar 10 2014 | 4:13 am
      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
    • Mar 10 2014 | 5:45 am
      Thanks again, Luigi. Now that was a dumb one. I think my brain is telling me to take a break ;).
      best,
      Zachary