Forums > Javascript

store more than 2000 characters @ external file

July 10, 2008 | 10:36 pm

hello all,

i am trying to save a string with more than 2000 character using javascript. inside javascript i can address and manipulate strings with a lenght of more than 90000 characters. but it is impossible to store the large string.

- the messagewindow outputs only 2000 characters
- the file i save contains 90000 characters, but the last 98000 are empty.
- max crashes, when i generate a string with more than 100000 characters

i use max 4.6.3

any help out there ?

here is my example script (just send a bang):

____________________________________________________

inlets = 1;
outlets = 1;
autowatch = 1;

function bang(){
this.thePatcherPath = this.patcher.filepath;

theOutString = "";
for ( var i=0; i<90000; i++ ){
theOutString = theOutString + "I";
}

for ( var i=0; i<1; i++ ){
theOutString = theOutString + "B";
}

thePath = this.thePatcherPath + "/" + "test.txt"
this.writeFile( thePath, theOutString );
}

function writeFile( whichFilePath, whichString ){
post( "n" + " last : " + whichString.charAt( 90000 ) + "n" );
post( "n" + " string : " + whichString + "n");
post( "n" + " length : " + whichString.length + "n");
f = new File( whichFilePath, "readwrite", []);
f.open( whichFilePath );
f.writestring( whichString );
f.eof = whichString.length;
f.close()
}


July 11, 2008 | 8:43 am

Hi,

I remember encountering the length limit for writestring (and perhaps
for readline as-well, can’t remember any more), and my workaround was
to write the string byte-by-byte – using writebytes/writechar.

don’t know about maximum string length, but if you need humongous
amounts of bytes, perhaps you could use jit.matrix?

hth,
nesa

On Jul 11, 2008, at 12:36 AM, videomasta wrote:
>
> – the file i save contains 90000 characters, but the last 98000 are
> empty.
> – max crashes, when i generate a string with more than 100000
> characters


July 11, 2008 | 9:21 am

found a solution !
just split the string into slices before saving ….
its a bit laborious …

here is the code:

function writeFile( whichFilePath, whichString ){
f = new File( whichFilePath, "readwrite", []);
f.open( whichFilePath );

theMaxChars = 1000;
theDivisions = whichString.length / theMaxChars;

for ( var i=0; i

f.position = ( i ) * theMaxChars;;
theString = whichString.slice(i*theMaxChars, (i+1)*theMaxChars);
f.writestring( theString );
}
f.eof = whichString.length;
f.close()
}


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