Subject: Re: Reading Text from a file Newsgroups: comp.sys.apple2.programmer From: dempson@actrix.gen.nz (David Empson) Date: Thu, 29 Jul 1999 21:39:59 +1200 Message-ID: <1dvq370.qnp79211ccr9kN@dempson.actrix.gen.nz> References: <7nnbsf$rn9$1@news.ccit.arizona.edu> Organization: Empsoft X-Newsreader: MacSOUP 2.3 NNTP-Posting-Host: 202.49.157.176 X-Original-NNTP-Posting-Host: 202.49.157.176 X-Trace: 29 Jul 1999 21:37:36 NZST, 202.49.157.176 Lines: 119 Path: lobby!newstf02.news.aol.com!portc02.blue.aol.com!howland.erols.net!news-out.digex.net.MISMATCH!dca1-hub1.news.digex.net!intermedia!news-dc.gip.net!news-stock.gip.net!news.gsl.net!gip.net!news.iprolink.co.nz!news.actrix.gen.nz!dempson Christopher J Lewandowski-pearson wrote: > Hello!! > > It has been many moons since I last had the pleasure of writing an > Apple ][ BASIC program and I hardly remember anything. Can someone > please answer the following questions: > > Say I have a text file transferred from a PC (ick!). How can I > open the file and read in lines?? Here is the guts of a text file read loop. 10 D$=CHR$(4) 20 PRINT D$;"OPEN MYFILE" 30 PRINT D$;"READ MYFILE" 40 ONERR GOTO 100 50 INPUT L$ 60 GOSUB 200: REM Parse line and do what you want with it 70 GOTO 50 100 POKE 216,0: REM Disable ONERR GOTO 110 E=PEEK(222): REM Error number 120 IF E<>5 THEN PRINT "ERROR #";E;" IN LINE "; PEEK(217) + 256 * PEEK(218) 130 PRINT D$;"CLOSE" 140 END 200 REM Your parser goes here 299 RETURN: REM Go back for the next line (I haven't verified those PEEK and POKE locations for error handling, or the error number for END OF DATA, but they should be about right if my memory hasn't completely gone.) (Line 120 is a single continuous line - I've just drawn it split over two lines to make the program easier to read.) Note that the text file format of the PC and Apple II are different. You should make sure the file is in Apple II format (which is the same as Mac format) before trying to process it. The signficant differences are: - PC uses CR/LF at the end of each line, while the Apple II uses CR only. - PC may put a Ctrl-Z character at the end of the file. This should be removed for the Apple II. - The last line should end with CR to ensure the program doesn't get stuck waiting for it (I don't think this is a problem, but better safe than sorry). If you haven't dealt with these issues then the first thing your parser should do is: 210 IF LEN(L$) <> 0 THEN IF ASC(LEFT$(L$, 1)) < 32 THEN L$ = MID$(L$, 2): GOTO 210 (This should be a single line in the program, wrapped as necessary.) This will strip any control characters which appear at the start of the line (the Line Feeds and/or Ctrl-Z will appear there). > If I remember correctly, there were two ways to open files.... Not as such. You can do it as above, treating the file as text input, or you can simply BLOAD the entire file into memory (under ProDOS only) and use PEEKs to access the content. This is a bit riskier, because you need to make sure you don't overwrite anything important with the BLOAD. > Can I just use POKE?? Say I want to move a 7 into $1000, can I do: > POKE(1000,7)?? (or was that on the Commodore??) Yes, but you need to use decimal numbers in Applesoft, so pokeing into $1000 would be POKE 4096,7. Don't put parentheses around the arguments (that only applies to functions, like PEEK, not statements). There are two important points here: 1. If you want to poke data into memory starting at $1000, then your BASIC program must be no longer than 2047 bytes, or you will overwrite the end of it. (This limits it to about five blocks under ProDOS.) You can reduce the program size by eliminating any REM statements and using single character variable names instead of double character ones. If you can't get the program this small, you may need to start poking your data in at $2000 (8192) or somewhere in between. 2. You must use the LOMEM: statement to set the start address for BASIC variables, to keep them out of the way of your buffer. This must be the first statement in the program. For example, if you want to allow for an 8K image buffer at $1000 to $2FFF, use the statement: 5 LOMEM:12288 If your buffer is 8K starting at $2000, then use 5 LOMEM:16384 If your buffer is 16K, add a further 8192 to either of these values. You probably won't be able to fit 32K without running out of memory for variables. > In case you are wondering, I want to be able to read in Motorola S-records > that contain 68000 machine code into the Apple ][s memory so that I can > then use the ultra cool eprom burner I have for the Apple to burn an > EPROM. Sounds like a good plan. (I have access to standalone EPROM programmers at work, so I've never had to muck about with Apple II ones.) > Any help would be appreciated (answers, web pages with info, book titles)!! Feel free to ask me any further questions, either here or in E-Mail. -- David Empson dempson@actrix.gen.nz Snail mail: P.O. Box 27-103, Wellington, New Zealand