CPCWiki forum

General Category => Programming => Topic started by: fewbytes on 20:49, 01 February 10

Title: Save to array and loading them , omg how to ??
Post by: fewbytes on 20:49, 01 February 10
I want to save and load a ascii file to a an array
How you do it in basic 1.1?

Thx in advance
Title: Re: Save to array and loading them , omg how to ??
Post by: Grim on 01:14, 02 February 10
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: [Select]
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
Title: Re: Save to array and loading them , omg how to ??
Post by: Octoate on 08:45, 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$).
Your example fetches the file char by char, because you only use INPUT a$ in line 20. I guess you meant LINE INPUT a$ :).
Title: Re: Save to array and loading them , omg how to ??
Post by: 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?! :)

Title: Re: Save to array and loading them , omg how to ??
Post by: Ygdrazil on 11:20, 02 February 10
LINE INPUT #9,a$?

/Ygdrazil

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?! :)
Title: Re: Save to array and loading them , omg how to ??
Post by: fewbytes on 13:16, 02 February 10
I made a drawing programs it saves all commands to a file with text so
the file has:
p x y
d x y
d x y

p is plot and d is draw x and y are numbers
and it has more plots if needed.
so far is ok but I load it i works i see the picture but then i draw more lines
file contents changes it get overwrited so I don't know how to save it
without overwriting it and adding more lines to the file.
I was told to load the file to an array but I don't know how.

I hope i xplained my self

Thx in advance
Title: Re: Save to array and loading them , omg how to ??
Post by: arnoldemu on 13:39, 02 February 10
I made a drawing programs it saves all commands to a file with text so
the file has:
p x y
d x y
d x y

p is plot and d is draw x and y are numbers
and it has more plots if needed.
so far is ok but I load it i works i see the picture but then i draw more lines
file contents changes it get overwrited so I don't know how to save it
without overwriting it and adding more lines to the file.
I was told to load the file to an array but I don't know how.

I hope i xplained my self

Thx in advance
Ok, well basic will store all data as "ascii" file type. But really your program needs to know the type of data you put into the file so that you can choose the correct basic commands to read and write the data.

Your original question seemed to say you wanted to read a "ascii" document, like a text file with words and sentences in it and to store this in basic.

The Amstrad will always write over the top of the data as you found. So you need to read the file, remember the data in your program, add to it, and then save the new complete data. In this way it is like you are adding to the file.

Based on your description of the data, you can do it better like this:
Code: [Select]
10 DIM cmd(200)
20 DIM x(200)
30 DIM y(200)
40 OPENIN"drawfile"
50 count = 0
60 WHILE NOT EOF
70 INPUT #9,cmd(count)
80 INPUT #9,x(count)
90 INPUT #9,y(count)
100 count = count+1
110 WEND
120 CLOSEIN
Line 10,20 and 30 define 3 arrays each holds numbers.

cmd(0) is the command for instruction 0
x(0) is the x coordinate for instruction 0
y(0) is the y coordinate for instruction 0
cmd(1) is the command for instruction 1
x(1) is the x coordinate for instruction 1
y(1) is the y coordinate for instruction 1
etc.

I call an instruction:
p x y
or
d x y
it is the collection of information for one drawing operation.
 
Array "cmd" holds the drawing command: e.g. p=0, d=1
array x holds the x coordinate
array y holds the y coordinate

Line 40 opens your file for reading.
line 50 remembers the number of instructions it is reading from the file and 1 is added each time a instruction is read.
The arrays hold up to 200 instructions.
 
line 60 keeps reading until the end of file condition is reached. (e.g. no more data in file)
line 70,80,90 read the command then the x coord, then the y coord for one instruction
line 100 increments the instruction count (also so it knows next position in each array to store data)
line 110 is the end of the reading loop
line 120 closes the input file

Ok, so this code will read in the data.

Now to save it you do this:

Code: [Select]
10 OPENOUT"drawfile"
20 for i=0 to count
30 PRINT #9,cmd(i)
40 PRINT #9,x(i)
50 PRINT #9,y(i)
60 NEXT
70 CLOSEOUT

Line 10 opens the file for writing - this will also destroy contents of file.
Line 20 starts a loop over all instructions stored in the arrays in your program.
Line 30,40,50 put the data to the file.
Line 60 finishes the loop
Line 70 closes the file. Now all data is in the file.

now to add a new plot command at 300,400 (x,y) you do this:

cmd(count)=0;
x(count)=300;
y(count)=400;