Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.
|
Forum Index : Microcontroller and PC projects : Help with File Handling
Author | Message | ||||
Nick Guru Joined: 09/06/2011 Location: AustraliaPosts: 512 |
Having trouble with some file handling. The following program saves the variables S$,A and B to a file called "TEST". S$="HELLO" A=100 B=55 Open "TEST" For OUTPUT As #1 Print #1,S$,A,B Close #1 Open "TEST" For INPUT As #1 Input #1,S$,A,B Close #1 Print S$,A,B When I read those variables back, I get... HELLO 100 55 0 0 S$ has combined the A and B variables. What's going on here? Nick |
||||
vk4tec Senior Member Joined: 24/03/2012 Location: AustraliaPosts: 239 |
OK here is what is going on On reading back, S$ = "HELLO 100 55" and A = "0" and B = "0" On reading back, it does not know what you mean how does it know how to chop up the data ? - Andrew - Andrew Rich VK4TEC www.tech-software.net |
||||
xraydude Newbie Joined: 12/01/2013 Location: United StatesPosts: 2 |
I believe you might need to use comma delimiting when writing to the file? i.e.: Open "TEST" For OUTPUT As #1 Print #1,S$,",",A,",",B Close #1 Ted Ted Forrest KB5CQF |
||||
vk4tec Senior Member Joined: 24/03/2012 Location: AustraliaPosts: 239 |
I would doubt it even knows to write A and B to a file. I normally convert variables A and B to "text" You may do to put commas and then split on the commas 1. Convert A and B to strings 2. Write with commas 3. Split on commas "HELLO,100,55" Then split read on commas. - Andrew - Andrew Rich VK4TEC www.tech-software.net |
||||
vk4tec Senior Member Joined: 24/03/2012 Location: AustraliaPosts: 239 |
Andrew Rich VK4TEC www.tech-software.net |
||||
vk4tec Senior Member Joined: 24/03/2012 Location: AustraliaPosts: 239 |
Or if you gurantee the sequence - write to lines 1,2,3 etc write s$ write A write B ( etc ) read s$ read A read B read s$ read A read B read s$ read A read B read s$ read A read B Andrew Rich VK4TEC www.tech-software.net |
||||
TassyJim Guru Joined: 07/08/2011 Location: AustraliaPosts: 6098 |
As pointed out by xraydude, you need to add the comma between items when printing if you want to read them in separately. S$="HELLO" A=100 B=55 Open "TEST" For OUTPUT As #1 Print #1,S$,",",A,",",B Close #1 Open "TEST" For INPUT As #1 Input #1,S$,A,B Close #1 Print S$,A,B Jim VK7JH MMedit MMBasic Help |
||||
shoebuckle Senior Member Joined: 21/01/2012 Location: AustraliaPosts: 189 |
Hi Nick, It took me a while to discover what was going on too. What is needed is either a CR or a comma between variables in the output stream to separate values. If you write multiple values in a single Print statement, you must add a "," between each, but not after the last one. If you put each variable in its own Print statement, a separator is automatically added. Below is the code I used in the Hearts & Bones game to load and save various parameters. You will see that I inserted a "," between each variable in the SaveGame subroutine. You don't need, and mustn't insert, a "," at the end of each statement as a separator is automatically inserted by the Print command unless you terminate the Print with a semi-colon. I found this out by writing some print statements and looking at the resulting file with a text processor. There are a couple of file dump programs in the library, HexDump and TextLook, which will show you what's in the output file. Cheers, Hugh LoadGame:
Print @(0,21*12) Space$(10) "Please enter your name so I can load your highest score," Print Space$(10) "starting number of bones and direction keys"; Input FName$ If Len(FName$)>8 Then FName$=Left$(FName$,8) FName$=FName$+".xls" Option Error Continue Open FName$ For input As #1 If MM.Errno=0 Then Input #1,HighScore Input #1,UL,UN,UA,UR,LN,LA ' direction key assignments Input #1,RN,RA,DL,DN,DA,DR,MK Input #1,StartBones Close #1 EndIf Option Error Abort GoSub ClearHelpText Return SaveGame: Open FName$ For output As #1 Print #1,HighScore Print #1,UL","UN","UA","UR","LN","LA ' direction key assignments Print #1,RN","RA","DL","DN","DA","DR","MK Print #1,StartBones Close #1 Return |
||||
panky Guru Joined: 02/10/2012 Location: AustraliaPosts: 1101 |
Hi Nick, Ran the test on DOS MMBasic v4 and in my tests it appears the issue is in the write to file area. The first Print #1,s$,a,b just writes all the variables as a continuous stream of text characters, converting the numeric variables to text with a leading space and ends with a trailing 0D 0D 0A When it is read back with the input statement, this whole string is read back in as s$, so it appears that the first variable in the input statement gets everything in the file. My reading of the MMBasic Manual indicates that this is probably correct although the use of multiple variables in the print #1 statement is useless. " On the SD card both data and programs are stored using standard text and can be read and edited in Windows, Apple Mac, Linux, etc. An SD card can have up to 10 files open simultaneously while the internal flash drive has a maximum of one file open at a time. " If you need to store multiple variables, you need to seperate them yourself in some way and maybe do some numeric to string and vica versa conversion yourself. eg. if your original print #1 statement were print #1,s$,",",a,",",b then a TAB COMMA TAB (09 2C 09) sequence would be inserted between the variables written to file and then the input statement will work correctly. Not sure if this is a correct interpretation however I am sure Geoff can sort it or explain what we are doing wrong. Cheers, Doug. ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |
||||
MOBI Guru Joined: 02/12/2012 Location: AustraliaPosts: 819 |
I wonder why the two carriage returns (OD OD). We used to use 2 CR in old teleprinter days because of the settling time of the print carriage to the left margin. I would have thought that modern tecnology was faster than that. On the file write/read multi variables, I found that with most languages if you want to separate variables on the one line, you have to manually put in delimeters as "," other wise the first read variable grabs the lot. David M. |
||||
Print this page |