Hi Oriol,
When File.read() reads only one item, it doesn't return it as an array, but as a single object. This happens because the prototype of File.read() is:
Object File.read( int dataType[, int count=1] )
Note that the count argument is optional. When count is not specified, count=1 is assumed.
Try with this version of your code, which works as expected:
#include <pjsr/DataType.jsh>
#define N 10
function main()
{
var userArray = new Array( N );
for ( var i = 0; i < N; ++i )
userArray[i] = i;
var fo = new File;
fo.createForWriting( "/home/juan/testFile.dat" );
fo.write( userArray, DataType_Int32 );
fo.close();
var fi = new File;
fi.openForReading( "/home/juan/testFile.dat" );
var userArray2;
var maybeUserArray = fi.read( DataType_Int32, N );
if ( maybeUserArray instanceof Array )
userArray2 = maybeUserArray;
else
{
userArray2 = new Array( 1 );
userArray2[0] = maybeUserArray;
}
for( var i = 0, n = userArray2.length; i < n; ++i )
console.writeln( "position ", i, " value ", userArray2[i] );
console.show();
}
main();
(you'll have to change /home/juan by /home/oriol). This code works for N = 1.