0
votes

I have a code (this is a part only) :

var iStr:String;
var kStr:String;

var pixelValue:uint;

var Xcord:Vector.<int> = new Vector.<int>();
var Ycord:Vector.<int> = new Vector.<int>();
var PixVal:Vector.<uint> = new Vector.<uint>();

 for (i= 0; i< 225; i++)
 {
  iStr = fs.readUTF()  + "\n";  
  Xcord[i] = int(iStr); 
  kStr = fs.readUTF()  + "\n";
  Ycord[i] = int(kStr);
  pixelValue = fs.readUnsignedInt() ;
  PixVal[i] = pixelValue;  
  trace(Xcord[i]);
  trace(Ycord[i]);
  trace(PixVal[i].toString(16));
  i++;
  trace(i);
}

I have three arrays, first and second ones for storing integers values, third one for storing unit values. I want after reading every line in my text file data , I assign it to an element in an array, but the problem, it reads only first 3 data (values) in the file, then give me this error message:

Error: Error #2030: End of file was encountered.

at flash.filesystem::FileStream/readUTF() at bitmapPixelDisplay_Air_Scene1_fla::MainTimeline/frame1()[bitmapPixelDisplay_Air_Scene1_fla.MainTimeline::frame1:63] at runtime::ContentPlayer/loadInitialContent() at runtime::ContentPlayer/playRawContent() at runtime::ContentPlayer/playContent() at runtime::AppRunner/run() at ADLAppEntry/run() at global/runtime::ADLEntry()

Can anybody help me to solve my problem? what is a proper way to load a text file data into an array? how to fix my code? Any way to check for reaching end of file? or Should I use byteArray, then I use readByte method and How? or what?.

with thankful.

I have gotten an idea for my issue, I like to give me your opinion, I have used ay array as follow:

var PixInfo: Array = new Array(21, 35,0x393939, 22, 33,0x4c4c4c, 22, 34,0x2e2b2b, 22, 35,0x141414, 22, 36,0x161313, .......... );

I have filled the array with all values from text file (by copy and paste method) then, I have updated my code as follow:

PixInfo.reverse(); // to read array in reverse way.

Canav.addEventListener(Event.ENTER_FRAME, TextDisplay);

function TextDisplay(evt: Event):void
{

    CanavData.setPixel(PixInfo[i+2], PixInfo[i+1], PixInfo[i]);

    trace (PixInfo[i+2], PixInfo[i+1], PixInfo[i].toString(16));

    i+=3;

    if ( i > 674)
  {
     Canav.removeEventListener(Event.ENTER_FRAME,TextDisplay);
  }

it has worked very well as I need, I think its short way for solving my issue, but any suggestion or advice for me about if this way has any disadvantages or problems?

2
It would be useful to see the text file that you're referring to. Additionally, I'm pretty sure your first call to readUTF() gives you the entire textfile as a string. You could parse that, depending on the structure of this text file. - user2655904
a part of my text file is : 21 35 393939 22 33 4c4c4c 73 31 191717 73 32 100e0e 73 33 151414 73 34 121212 73 35 c0c0c 73 36 272727 74 28 434343 74 29 392d2d 74 31 322c2c 74 32 c0404 74 35 1a1a1a 74 36 7f0000 - Ayman Radwan
Alright, that's good. Call readUTF() once, use split(" ") on the string you get from it and then simply loop through the given array and sort it into other arrays/vectors however you need it. - user2655904

2 Answers

0
votes

Edit Answer : After re-reading I see you really mean...

A part of my text file is :

21 35 393939 
22 33 4c4c4c 
73 31 191717 
73 32 100e0e 
73 33 151414 
73 34 121212 
73 35 c0c0c //<---needs fixing as c0c0c0??
73 36 272727 

You can use the spaces (byte 0x20) as a delimiter, to know the end/start of the required values.

First set up your vars like below:

public var iStr:String;
public var kStr:String;

public var pixelValue:uint;

public var Xcord:Vector.<int> = new Vector.<int>();
public var Ycord:Vector.<int> = new Vector.<int>();
public var PixVal:Vector.<uint> = new Vector.<uint>();

//add these
public var temp_BA :ByteArray = new ByteArray; //holds some byte values of file
public var byteVal :int = 0;
public var pos_Start :int = 0;
public var pos_End :int = 0;

Then later you can use a while loop to update :

fs.position = 0; //important!!
var idx:uint = 0; //your index/pos within your Vector arrays

while (true)
{
    if ( ( (pos_Start+1) > fs.length ) || (fs.position >= fs.length) )
    { trace("### Reached end of file... breaking"); break; }
    else
    {
        //# get X-coords
        find_Space_delimiter(pos_Start);
        temp_BA.position = 0;
        Xcord[idx] = int( String(temp_BA) );
        trace("Xcord["+idx+"] : " + Xcord[idx]);

        //# get Y-coords
        pos_Start += 1; //update pos
        find_Space_delimiter(pos_Start);
        temp_BA.position = 0;
        Ycord[idx] = int( String(temp_BA) );
        trace("Ycord["+idx+"] : " + Ycord[idx]);

        //# get PIXel-VALue
        pos_Start += 1; //update pos
        find_Space_delimiter(pos_Start);
        temp_BA.position = 0;
        PixVal[idx] = int( "0x" + String(temp_BA) );
        trace("PixVal["+idx+"] : " + PixVal[idx].toString(16) );

        pos_Start += 1; idx++; //update positions
    }
}

You'll need this helper function to scan for any bytes until stopping at a space (0x20) byte...

public function find_Space_delimiter(inPos:int) : void 
{
    temp_BA.clear(); //reset
    fs.position = pos_Start = inPos;

    while(true)
    {
        if (fs.position < fs.length)
        { byteVal = fs.readUnsignedByte(); }  //position is now +1 (auto after any READ)
        else{ break; }

        //# is space
        if (byteVal == 0x20) 
        { 
            //trace("got entry at : " + fs.position + " ... breaking"); 
            temp_BA.position=0; break; 
        }
        else 
        {
            //trace("byteVal : " + byteVal.toString(16).toUpperCase() );

            temp_BA.writeByte(byteVal);
            pos_Start++;
        }
    }

}

Original answer

A part of my text file is : 21 35 39 39 39 22 33 4c 4c ....

So you've got hex values in your text, and you want to convert them into a ByteArray?

Try this:

var my_text :String = "your text file contents";
var my_bytes_Array :ByteArray = new ByteArray;

//# update the Array with values converted from text
my_bytes_Array = string_toBytes (my_text);

//# the converter function (returns/updates data type of... ByteArray)
public function string_toBytes( hex:String ) :ByteArray
{
    // CONVERT STRING OF HEX CODES INTO A BYTEARRAY //
    /////////////////////////////////////////////////

    hex = hex.split(" ").join(""); //remove white spaces in bytes string

    var ba:ByteArray = new ByteArray();     ba.position = ba.length;
    var len:uint = hex.length;              //trace("writeBytes. len: " + len);

    for (var i:uint = 0; i < len; i += 2)
    {
        var byte:uint = uint("0x" + hex.substr(i, 2));
        ba.writeByte(byte);
    }

    return ba;
}
0
votes

I might be missing something but in case the fs is a filestream it should be much easier?

var myString:String = fs.readUTF();

var lines:Array = myString.split("\n"); // split text file by newlines. Your textfile do have the values on each line right?

for(var i:int; i < lines.length; i++)
{
    var lineArray:Array = lines[i].split(" "); // split each line by spaces. if you have tabs split by "\t"

    var x:int = trim(lineArray[0]) as int;
    var y:int = trim(lineArray[1]) as int;
    var pixVal:int = trim("0x" + lineArray[2]) as int; // prepend with 0x so it will cast to an integer. Untested though
}

private function trim(value:String):String
{
    // remove all spaces
    return value.reaplce(/ /gi, value);
}