READ command query

3 replies [Last post]
Offline
Joined: Sep 4 2014
Posts: 21

Hi. This is me seeking code help again. I do usually look in manuals (which are vague and rarely provide examples). Then I try to backwards engineer other programs. Then I try to create my own small routines.

But...

The READ command.

1) Usually I use OPEN / READ / CLOSE to get information from a text file. Works fine. However, I've noticed other programs seem to just use READ. For example,
"READ PROGRAM":INPUT A$
Can I just READ PROGRAM or do I need OPEN/READ/CLOSE?

2) Is it possible to read one certain variable from a text file? I've seen things like "READ PROGRAM,R" : INPUT A$ (without OPEN/CLOSE as above)

Lastly, how does READ work with DATA
3) I've tried various ways to read data. How does a simple read data work?

4) How do I read only certain portions of DATA. For instance if I have a list of, lets say, the 12 months of the year.
DATA "JANUARY", "FEBRUARY", "MARCH", "APRIL", and so on...
How can I read just the first three variables, and then later in the program read the second set of three months. Or read the first six months, and the later the last six months.
Linking with the second question, will that work with a TEXT file?

5) Data can be more than one line, correct? EG:
100 DATA "DOG","CAT","HORSE"
110 DATA "COW","PIG","RABBIT"
120 DATA "FROG", "MONKEY", "SEA CUCUMBER"
I don't have any examples for 5) as I'm trying to work out how to make more efficient programming. Except to say each line could contain 15 or so animals instead of three.

6) Data can be numbers and words?
100 DATA "DOG", 101, "CAT", 3.1415, "HORSE", 1962

Thanks. Smile

__________________

"You can be in my dream if I can be in yours" / Bob Dylan

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Offline
Joined: Apr 11 2006
Posts: 1013
Re: READ command query

RockbadgerX wrote:

I do usually look in manuals (which are vague and rarely provide examples).

You are reading the wrong manuals, then. The Applesoft and DOS manuals are models of useful writing, with good and useable examples. Look for them on http://www.1000bit.it/support/manuali/manuali.asp by selecting Apple Computer, Inc. from the manuals section. They will answer your questions and more importantly - educate you on what you're trying to do.

Applesoft: http://www.1000bit.it/support/manuali/download.asp?id=534
DOS 3.2 (close enough for BASIC purposes): http://www.1000bit.it/support/manuali/download.asp?id=711

RockbadgerX wrote:

The READ command.

1) Usually I use OPEN / READ / CLOSE to get information from a text file. Works fine. However, I've noticed other programs seem to just use READ. For example,
"READ PROGRAM":INPUT A$
Can I just READ PROGRAM or do I need OPEN/READ/CLOSE?

If you do a READ by itself, it will seek a DATA statement to pull data from. If you want to read from a file named PROGRAM, it would have to be prefixed with CHR$(4); that means that a subsequent INPUT statement will pull data from that file. READ pulls data from a DATA statement; CHR$(4);"READ ..." (from DOS) prepares the system to pull data from a sequential file with an INPUT statement. Same statement, but different contexts and different actions.

RockbadgerX wrote:

2) Is it possible to read one certain variable from a text file? I've seen things like "READ PROGRAM,R" : INPUT A$ (without OPEN/CLOSE as above)

Then it's reading from DATA statements. Again, if it's not prefixed with CHR$(4), it's not going to the disk drive.

RockbadgerX wrote:

Lastly, how does READ work with DATA
3) I've tried various ways to read data. How does a simple read data work?

Please have a look at the Applesoft book (Chapter 6 - I/O commands go over READ and DATA). And play with it. I think you're getting confused between file-based READ and BASIC-based READ/DATA.

RockbadgerX wrote:

4) How do I read only certain portions of DATA. For instance if I have a list of, lets say, the 12 months of the year.
DATA "JANUARY", "FEBRUARY", "MARCH", "APRIL", and so on...
How can I read just the first three variables, and then later in the program read the second set of three months. Or read the first six months, and the later the last six months.

Applesoft keeps track of how many DATA items have been READ so far. If you only READ six, the other six will lie in wait until you READ them. If you try to do a READ 13 times (and you only have 12 data items), you will get an OUT OF DATA error. Unless you rewind with the RESTORE statement.

RockbadgerX wrote:

Linking with the second question, will that work with a TEXT file?

Mostly, yes. But of course if you leave a file open, INPUT and PRINT will be happening on the file rather than the keyboard/screen until it is closed. Again, read about the ways the BASIC and DOS variants work in their respective books. And play with it.

RockbadgerX wrote:

5) Data can be more than one line, correct? EG:
100 DATA "DOG","CAT","HORSE"
110 DATA "COW","PIG","RABBIT"
120 DATA "FROG", "MONKEY", "SEA CUCUMBER"
I don't have any examples for 5) as I'm trying to work out how to make more efficient programming. Except to say each line could contain 15 or so animals instead of three.

Yes. DATA statements all add up to one big bag of data that is can be READ (in line-number order) until they're exhausted (or RESTOREd) no matter what their line numbers are.

RockbadgerX wrote:

6) Data can be numbers and words?
100 DATA "DOG", 101, "CAT", 3.1415, "HORSE", 1962

Yes, but understand that you have to READ them in order with the matching kind of variable - so you need to READ them with READ A$,A to get a string and a number. If they don't match (i.e. READ A,A$) you will run into unexpected results.

Dog Cow's picture
Offline
Joined: Dec 11 2008
Posts: 461
Re: READ command query

RockbadgerX wrote:

The READ command.

1) Usually I use OPEN / READ / CLOSE to get information from a text file. Works fine. However, I've noticed other programs seem to just use READ. For example,
"READ PROGRAM":INPUT A$
Can I just READ PROGRAM or do I need OPEN/READ/CLOSE?

This was an undocumented feature of DOS 3.3. Officially, you are supposed to OPEN the file before you READ from it, but you can skip OPEN with DOS 3.3.

In ProDOS, the OPEN is mandatory.

Under both operating systems, you should always use CLOSE when you're finished with the file.

But hey, don't take my word for it. Here are two companion programs. Run the one, then the other.

]LOAD WRITE
]LIST

10 D$ = CHR$ (4)
20 PRINT D$;"OPEN FILE"
30 PRINT D$;"WRITE FILE"
35 PRINT "FOOBAR"
40 PRINT D$;"CLOSE FILE"

]

]LOAD READ
]LIST

10 D$ = CHR$ (4)
20 PRINT D$;"READ FILE"
30 INPUT A$
35 PRINT D$;"CLOSE FILE"
40 PRINT A$

]

__________________

Email me: dog_cow@m acgui.com
Mac GUI Vault: http://macgui.com/vault/
Apple II Book: http://macgui.com/newa2guide/
Inside Allerton Park book

Offline
Joined: Sep 4 2014
Posts: 21
Re: READ command query

I actually noticed this in other programs.

Dog Cow wrote:

10 D$ = CHR$ (4)
20 PRINT D$;"READ FILE"

I tried doing it but it didn't work Sad
You seem to have answered as to why. Thanks.

__________________

"You can be in my dream if I can be in yours" / Bob Dylan