General Category > Programming

Save to array and loading them , omg how to ??

(1/2) > >>

fewbytes:
I want to save and load a ascii file to a an array
How you do it in basic 1.1?

Thx in advance

Grim:
A string variable (stored in an array or not) is max. 256 char long. Unless the data in your ascii file are very short (you didn't give many details), you can't load the whole thing into a single element of an array (if this is what you want). But you can split your data into multiple array elements (like the example below does).

The following program will fetch data from an ascii file, line by line, and store each line (textline$) into a separate element (item%) of an array (array$).


--- Code: ---10 OPENIN "yourfile.txt":DIM array$(1000):item%=1
20 INPUT #9,textline$:IF EOF THEN 50
30 array$[item%]=textline$:item%=item%+1:IF item%>1000 THEN 50 
40 GOTO 20
50 ' display all items in the array
60 CLOSEIN
70 FOR i%=1 TO item%-1:PRINT array$[i%];:NEXT

--- End code ---

Octoate:

--- Quote from: Grim on 01:14, 02 February 10 ---The following program will fetch data from an ascii file, line by line, and store each line (textline$) into a separate element (item%) of an array (array$).

--- End quote ---
Your example fetches the file char by char, because you only use INPUT a$ in line 20. I guess you meant LINE INPUT a$ :).

Grim:
Darn! As I didn't tested the example, that's quite possible. So I just did a quick test now, it seems to work with INPUT #9,a$ as well, ie. it fetches one line of the text-file, not only one char (see the attached screenshot).

But now I wonder... how the hell do I do to fetch only one char at a time then?! :)

Ygdrazil:
LINE INPUT #9,a$?

/Ygdrazil


--- Quote from: Grim on 09:04, 02 February 10 ---Darn! As I didn't tested the example, that's quite possible. So I just did a quick test now, it seems to work with INPUT #9,a$ as well, ie. it fetches one line of the text-file, not only one char (see the attached screenshot).

But now I wonder... how the hell do I do to fetch only one char at a time then?! :)

--- End quote ---

Navigation

[0] Message Index

[#] Next page

Go to full version
Powered by SMFPacks Media Embedder
Powered by SMFPacks Alerts Pro Mod
Powered by SMFPacks Mentions Pro Mod