javascript file I/O....


    May 19 2006 | 8:52 pm
    I am working on a library that will do generic file I/O. I have been having a little trouble reading data back. The following code snippet does not seem to return any data.
    function ReadInt() { if(f == null) return;
    var data ; data = f.readint32(1); if(data == null) { post("No data"); return; }
    post("got data"); outlet(0, data [0]); }
    It is says "got data" but nothing is sent out. The file position is updated each time I call ReaadInt(). Anyone have an idea what the problem might be?
    Anthony

    • May 22 2006 | 3:07 pm
      Anybody have any ideas?
    • May 22 2006 | 3:44 pm
      Hi,
      I just check and it seems inconsistent (at least not coherent with the documentation) because:
      data = f.readint32(1) returns a number
      and
      data = f.readint32(2) returns an object (an Array)
      so the following works here:
      data = f.readint32(1); if(data == undefined) { post("No data"); return; } post("got data"); outlet(0, data);
      Best, ej
    • May 22 2006 | 3:50 pm
      Actually the documention says that readint32 returns an array. So I assumed that f.readint32(1) would return an array of size 1.
      Anthony
    • May 22 2006 | 3:53 pm
      On 22 mai 06, at 17:07, apalomba@austin.rr.com wrote:
      > Anybody have any ideas?
      What happens if you return a rather than a[0] ?
      _____________________________ Patrick Delges
      Centre de Recherches et de Formation Musicales de Wallonie asbl http://users.skynet.be/crfmw/max
    • May 22 2006 | 4:10 pm
      If you return data, as opposed to data[0], it works. The documentation does not specify this behavior. Thanks for your help.
      Anthony
    • May 22 2006 | 5:49 pm
      Another quick question, I am trying to write some floats to a file. When I write any fractional numbers, it looks like they are being converted into integers.
      For example WriteFloat 0.5 is written to the file as a zero. This is the function I am calling:
      function WriteFloat(FloatData) { if(f == null) return;
      f.writefloat32(FloatData); }
      How can I get it to just write the data and not convert it?
      Anthony
    • May 22 2006 | 10:30 pm
      On 22-mai-06, at 18:10, apalomba@austin.rr.com wrote:
      > If you return data, as opposed to data[0], it works.
      :) I don't know why I proposed you to return "a" rather than "data". Sorry!
      p
    • May 22 2006 | 10:40 pm
      On May 22, 2006, at 10:49 AM, apalomba@austin.rr.com wrote:
      > Another quick question, I am trying to write some > floats to a file. When I write any fractional numbers, > it looks like they are being converted into integers.
      I am unable to reproduce. Tthe following example demonstrates writefloat32() and readfloat32() functions working under MaxMSP 4.5.7 on OS X.4.6. It could be that you are trying to exploit the byteorder attribute, which is unfortunately broken in 4.5.7, but fixed in the next version. However you haven't provided us with enough detail to examine your problem. As *always*, please provide a clear and simple example which illustrates your problem with clear steps to reproduce, and software and operating system information. Otherwise, there's less of a chance that you find the help you're looking for.
      function bang() { var f_out = new File("./outputtest.bin","write");
      if (f_out.isopen) { f_out.writefloat32(0.5); f_out.writefloat32(17.93); f_out.writefloat32(2.); f_out.close(); }
      var f_in = new File("./outputtest.bin","read");
      if (f_in.isopen) { post("in[0]" + f_in.readfloat32(1) + "n"); post("in[1]" + f_in.readfloat32(1) + "n"); post("in[2]" + f_in.readfloat32(1) + "n"); f_in.close(); } }
      -Joshua
    • May 23 2006 | 3:28 pm
      Thanks for the example Joshua, as it turns out the problem was bad message box formatting.
      Thanks, Anthony