Skip to main content
Known Participant
September 16, 2013
Answered

cannot seem to read a plain text file

  • September 16, 2013
  • 1 reply
  • 3005 views

this is actually macromedia flash mx 6.0, which I believe runs AS2 but is compatible with AS3

file = date.txt

file contents: xdate=12/01/2013

the file is in same directory as the .swf

code to read the file contents into a variable:

DateFile = new LoadVars();

DateFile.onLoad = function(ok) {

if (ok) {

var theDate = DateFile.xdate;

}

};

DateFile.load("date.txt");


theDate variable is empty

also tried this

file = date.txt

file contents: &xdate=12/01/2013&

DateFile = new LoadVars();

DateFile.load("date.txt");

var theDate = DateFile["xdate"];


This topic has been closed for replies.
Correct answer kglad

so sorry my dumb typo, I was consistent in my actual code, there doesn't seem to be a method that works

kinda frustrated right now, especially since existing app is retrieving multiple entries from its text file using

an index in [ ] brackets

I will experiment with no &'s and putting more than one line in the file

file content: &xDate=12012013&       (getting more ready to parse date if I can ever read the file)

var xDateFile = new LoadVars();

var xDatea:string;

var xDateb:string;

xDateFile.onLoad = function(ok) {

          if (ok) {

          xDatea = xDateFile.xDate;

          trace(xDatea);

          xDateb=this.xDate;

          trace(this.xDateb);

          }

};

xDateFile.load("xDate.txt");

var idx = "xDate";

txtTestBox.text = xDateFile[idx];

txtTestBox2.text = xDatea;

txtTestBox3.text = xDateb;

txtTestBox4.text = "so I know I got here";


loading a file is asynchronous, so except your last textfield, none of the other will contain the text you expect.  the trace statements should be ok though. 

is that what you see?

1 reply

kglad
Community Expert
Community Expert
September 16, 2013

that's not compatible with as3.

if you're publishing for as3, you should be seeing error messages.

are you publishing for as3?  are you seeing error messages?  do you want to publish for as3 or as2?

bbxrider1Author
Known Participant
September 16, 2013

thanks for the reply, this should be easy...right?

I think its as2, see below, it doesn't say which action script just the flash, which this version 6 I always thought was as2

publish does not give any errors. I 'm copying existing code that works to select one of many 'variables' in the 'loadvars'

text file, eg,

&var1=some text&

&var2=some text&

&var3=some text&

var index = "var2";

var foo = DateFile[index];

my new file only has 1 value so loadvars is overkill but thinking no harm done

it would appear loadvars creates an array indexed by the value to the left of the "="

I'm not sure why the existing app uses the "&" for each line in the text file

kglad
Community Expert
Community Expert
September 16, 2013

you're using as2.

use:

file contents: &xdate=12/01/2013&

/////////////////////////

var DateFile:LoadVars = new LoadVars();

DateFile.onLoad = function(ok) {

if (ok) {

trace(this.xdate);

doWhateverF(this.xdate);

}

};

DateFile.load("date.txt");

function doWhateverF(s:String):Void{

// do whatever with s (which will be "12/01/2013", a string).  if you're trying to coerce that into a Date instance, you'll need to parse the string.

}