Skip to main content
Inspiring
April 7, 2011
Answered

File.read() fails with binary file

  • April 7, 2011
  • 2 replies
  • 3824 views

this is annoying me as I thought this would be trivial (and according to the docs, completely possible).

I am opening a tiff file, and I am trying to read all the data.  I set encoding to binary after opening.heres my simple code:

var file = File.openDialog('select file');
file.open("r");
file.encoding = "binary";
alert(file.read());

I am using this file:

https://area51.d4creative.com/cgi-bin/fastLink.cgi?linkID=922&fileID=5162&keyCode=9tlpQ3dS

my alert box says: MM if I open that file in a text editor, there is more data after MM.  What is the problem?  Can extendscript really not properly read binary data despite the manuals claim that it can?

Mike Cardeiro

This topic has been closed for replies.
Correct answer Dan Ebberts

If I do it this way:

{
    var myFile = File.openDialog("Select binary file.");
    myFile.open("r");
    myFile.encoding = "BINARY";

    var myChar, myByte;
    var i = 0;
    s = "";
    while (! myFile.eof){
        myChar = myFile.readch();
        myByte = myChar.charCodeAt(0).toString(16);
        if (myByte.length < 2) myByte = "0" + myByte;
        s += myByte + " ";
        if (i%16 == 15) s += "\r";
        i++;
    }
    myFile.close();
    $.writeln(s);
}

This is what I get:

4d 4d 00 2a 00 00 00 08 00 0c 00 fe 00 04 00 00
00 01 00 00 00 00 01 00 00 03 00 00 00 01 00 04
00 00 01 01 00 03 00 00 00 01 00 04 00 00 01 02
00 03 00 00 00 03 00 00 00 9e 01 03 00 03 00 00
00 01 00 01 00 00 01 06 00 03 00 00 00 01 00 02
00 00 01 11 00 04 00 00 00 01 00 00 00 bc 01 15
00 03 00 00 00 01 00 03 00 00 01 16 00 03 00 00
00 01 00 04 00 00 01 17 00 04 00 00 00 01 00 00
00 30 01 1c 00 03 00 00 00 01 00 01 00 00 86 49
00 01 00 00 00 18 00 00 00 a4 00 00 00 00 00 08
00 08 00 08 38 42 49 4d 04 28 00 00 00 00 00 0c
00 00 00 01 3f f0 00 00 00 00 00 00 ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff

Dan

2 replies

Inspiring
April 7, 2011

even more puzzling (and infuriating), if I modify the code to this:

var file = File.openDialog('select file');
file.open("r");
file.encoding = "binary";
for(x=0;x<file.length;++x){
     file.seek(x,0);
     alert(file.readch());
}

it works as expected, I get an alert box for every character in the file; but the moment I try to save it like so

var file = File.openDialog('select file');
file.open("r");
file.encoding = "binary";
var t = new Array;
for(x=0;x<file.length;++x){
     file.seek(x,0);
     alert(file.readch());
}
alert(t);

i get M,M, in the alert box.  what the F*CK is going on with this...this should be trivial.

Dan Ebberts
Community Expert
Dan EbbertsCommunity ExpertCorrect answer
Community Expert
April 7, 2011

If I do it this way:

{
    var myFile = File.openDialog("Select binary file.");
    myFile.open("r");
    myFile.encoding = "BINARY";

    var myChar, myByte;
    var i = 0;
    s = "";
    while (! myFile.eof){
        myChar = myFile.readch();
        myByte = myChar.charCodeAt(0).toString(16);
        if (myByte.length < 2) myByte = "0" + myByte;
        s += myByte + " ";
        if (i%16 == 15) s += "\r";
        i++;
    }
    myFile.close();
    $.writeln(s);
}

This is what I get:

4d 4d 00 2a 00 00 00 08 00 0c 00 fe 00 04 00 00
00 01 00 00 00 00 01 00 00 03 00 00 00 01 00 04
00 00 01 01 00 03 00 00 00 01 00 04 00 00 01 02
00 03 00 00 00 03 00 00 00 9e 01 03 00 03 00 00
00 01 00 01 00 00 01 06 00 03 00 00 00 01 00 02
00 00 01 11 00 04 00 00 00 01 00 00 00 bc 01 15
00 03 00 00 00 01 00 03 00 00 01 16 00 03 00 00
00 01 00 04 00 00 01 17 00 04 00 00 00 01 00 00
00 30 01 1c 00 03 00 00 00 01 00 01 00 00 86 49
00 01 00 00 00 18 00 00 00 a4 00 00 00 00 00 08
00 08 00 08 38 42 49 4d 04 28 00 00 00 00 00 0c
00 00 00 01 3f f0 00 00 00 00 00 00 ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
ff ff ff ff ff ff ff ff ff ff ff ff

Dan

Inspiring
April 7, 2011

Dan, are you on mac or pc?  I saw an old thread (on aeenhancers) where this stuff was fine on mac but not so on pc

Dan Ebberts
Community Expert
Community Expert
April 7, 2011

When I run this, I get 236 which indicates to me that myBuffer has all the data from the image:

{

var myFile = File.openDialog("Select binary file.");

myFile.open("r");

myFile.encoding = "BINARY";

var myBuffer = myFile.read();

myFile.close();

alert(myBuffer.length);

}

Dan