# Read from file

**URL:** <https://forum.kirupa.com/t/read-from-file/10999>\
**Category:** Uncategorized\
**Created:** [January 10, 2003, 2:09pm UTC](https://forum.kirupa.com/t/read-from-file/10999 "2003-01-10T14:09:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![zkac050](https://avatars.discourse-cdn.com/v4/letter/z/b2d939/32.png) [@zkac050](https://forum.kirupa.com/u/zkac050)\
**Post date:** [January 10, 2003, 2:09pm UTC](https://forum.kirupa.com/t/read-from-file/10999/1 "2003-01-10T14:09:16Z")

</div>

how do you read in string variables from a txt file, and display this in a text box, so that the variables may be of use later.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 2:28pm UTC](https://forum.kirupa.com/t/read-from-file/10999/2 "2003-01-10T14:28:19Z")

</div>

Welcome, man.  
Check the tutes. There’s one about scrolling imported text that shows how you can do that.

pom :cowboy:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 2:33pm UTC](https://forum.kirupa.com/t/read-from-file/10999/3 "2003-01-10T14:33:15Z")

</div>

thanks for the reply, but the thing is :-  
lets say i have an output text box called “outFile”, it requires me to use “outFile=hello&”, within the text file itself, is there any way of getting around this where i dont need to use “outFile=”.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 2:40pm UTC](https://forum.kirupa.com/t/read-from-file/10999/4 "2003-01-10T14:40:00Z")

</div>

Well, yeah, read the tute, kid. You use a loadVars object, and you can do whatever you like in the onLoad.

pom :moustache

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 2:49pm UTC](https://forum.kirupa.com/t/read-from-file/10999/5 "2003-01-10T14:49:39Z")

</div>

sorry i didnt make myself clear, i read the tutorials and used loadVariables(url,target)  
but it seems that the actually content of the file is required to consist of an assignment of the target variable.  
outFile= …&  
what do you mean by using the onLoad as it only returns a boolean value.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:11pm UTC](https://forum.kirupa.com/t/read-from-file/10999/6 "2003-01-10T15:11:01Z")

</div>

so you want to be able to load an external txt file without it containing **thisTEXT=“content”**??? If so… Um, may I ask why?

Peace

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:15pm UTC](https://forum.kirupa.com/t/read-from-file/10999/7 "2003-01-10T15:15:39Z")

</div>

so i can use the variables for comparison

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:19pm UTC](https://forum.kirupa.com/t/read-from-file/10999/8 "2003-01-10T15:19:19Z")

</div>

> \*Originally posted by zkac050 \*  
> \*\*sorry i didnt make myself clear, i read the tutorials and used loadVariables(url,target)

// not loadVariables, loadVars

what do you mean by using the onLoad as it only returns a boolean value.

// err… WHA’?\*\*  
The tute: [http://www.kirupa.com/developer/mx/dynamic\_scroller.asp](http://www.kirupa.com/developer/mx/dynamic_scroller.asp)

The idea:

```auto
loadText = new loadVars();
loadText.load("kirupa.txt");
//creating the loadVarsText object and load function
loadText.onLoad = function() {
	// the variable container in the text is called Kirupatext
	// but you retrieve it as scroller.text.
	scroller.text = this.kirupatext;
};

```

Is that what you are trying to do?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:24pm UTC](https://forum.kirupa.com/t/read-from-file/10999/9 "2003-01-10T15:24:50Z")

</div>

yeah thanks K, i found and used :-  
function fileRead() {

```
loadVarsText = new loadVars();
loadVarsText.load("textfile.txt");
loadVarsText.onLoad = function() {
	outFile=loadVarsText;
};

```

}

but i get:-

there%20%20was%20a%20young%20man%20from%20downducket%20with%20a%20bob&onLoad=%5Btype%20Function%5D

from the text: "there was a young man from downducket with bob. "  
What is all that other stuff in between, and the text added onto the end, something to do with onload.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:34pm UTC](https://forum.kirupa.com/t/read-from-file/10999/10 "2003-01-10T15:34:20Z")

</div>

Strange. Use unescape:

```auto
a="there%20%20was%20a%20young%20man%20from%20downducket%20with%20a%20bob&onLoad=%5Btype%20Function%5D"
trace (unescape(a));

```

pom :pirate:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:42pm UTC](https://forum.kirupa.com/t/read-from-file/10999/11 "2003-01-10T15:42:43Z")

</div>

It seems to insert information about the onload[type function] to the end of the string, which i dont want. how do i stop this happening.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:50pm UTC](https://forum.kirupa.com/t/read-from-file/10999/12 "2003-01-10T15:50:54Z")

</div>

Try this:

myFile = new LoadVars();  
myFile.onData = function(rawDataFromMyFile){  
myText = unescape(rawDataFromMyFile);  
}  
myFile.load(“myTextFile.txt”);

That’ll override the normal onData handler (which overrides the procedures that look for variable/data pairs in your file) and simply drop the raw text data (minus escape characters) into the variable: myText.

You may want to put the following line after all that to un-override the onData method for the myFile object if you want to load other files in the usual way (with variable/data pairs).

delete myFile.onData;

Cheers.  
-Al

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 3:55pm UTC](https://forum.kirupa.com/t/read-from-file/10999/13 "2003-01-10T15:55:58Z")

</div>

thanks i tried this:-

myFile = new LoadVars();  
myFile.onData = function(rawDataFromMyFile){  
myText = unescape(rawDataFromMyFile);  
}  
outFile=myFile.load(“textfile.txt”);

but all it returns is true:-NOTE "outFile"is my output box.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 4:08pm UTC](https://forum.kirupa.com/t/read-from-file/10999/14 "2003-01-10T16:08:42Z")

</div>

all it prints out is " True"

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 4:13pm UTC](https://forum.kirupa.com/t/read-from-file/10999/15 "2003-01-10T16:13:35Z")

</div>

You need to put:

myFile = new LoadVars();  
myFile.onData = function(rawDataFromMyFile){  
outFile = unescape(rawDataFromMyFile);  
}  
myFile.load(“textfile.txt”);

🙂

-Al

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 10, 2003, 4:15pm UTC](https://forum.kirupa.com/t/read-from-file/10999/16 "2003-01-10T16:15:48Z")

</div>

Thank the almighty god, alert the church elders, we have a genius in the house, thank A, you are a life saver.  
:beam:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 13, 2003, 8:08pm UTC](https://forum.kirupa.com/t/read-from-file/10999/17 "2003-01-13T20:08:02Z")

</div>

Why not use xml?

```php
myXML = new XML();
myXML.onLoad = function (success) {
   if (success){
      trace ("loaded successfully");
      myTextBox.text = unescape(myXML);
   }else{
      trace("Error Loading Specified File");
   }
}
myXML.load ("Textfile.txt");

```

onData does work, but using the way above, you get the error checking feature integrated with hassle-free loading.

cheers;

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 13, 2003, 8:23pm UTC](https://forum.kirupa.com/t/read-from-file/10999/18 "2003-01-13T20:23:13Z")

</div>

```php

myLoadVars = new LoadVars();
myLoadVars.onLoad = function (success) {
   if (success){
      trace ("loaded successfully");
      myTextBox.text = this.myvar;
   }else{
      trace("Error Loading Specified File");
   }
}
myLoadVars.load ("Textfile.txt");

```

:-\

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 13, 2003, 8:27pm UTC](https://forum.kirupa.com/t/read-from-file/10999/19 "2003-01-13T20:27:02Z")

</div>

Hi senocular, just try the code above and you’ll see what i meant.

cheers;

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [January 13, 2003, 8:48pm UTC](https://forum.kirupa.com/t/read-from-file/10999/20 "2003-01-13T20:48:20Z")

</div>

you lost me
