Posted: Fri Sep 18, 2015 3:26 am Post subject: PUT
After struggling to find exactly how "PUT" works with a file, I have come to the following conclusions after some testing.
In relation to the little test program below. ( I have an SD card connected to MISO & MOSI and
am writing and reading from the card )
Code:
dim myfilename as fred.txt" 'file name
dim mytxt as string*20 'some random text
dim flength as long 'file length
dim n as long 'pointer
dim tempb as byte 'temporary byte
do
chdir "\" 'to root dir
open myfilename for binary as #128
'flength = filelen(myfilename)+1
mytext = "hello neil"
put #128 , mytxt
mytxt = "goodby"
put #128 , mytxt
flush #128
close #128
waitms 5000
'now see what has been written
print #1 , ""
print #1 , "reading file"
open myfilename for binary as #128
flength = filelen(myfilename)
for n = 1 to flength
get #128 , tempb , n
print #1 , "var=" ; chr(tempb)
next
close #128
loop
This program will write "hello neil" followed by 10 empty locations at the start of the file each time it goes through the loop, and it will append "goodby" followed by 14 empty cells after the word "goodbye"
Lesson:
PUT will overwrite the file the first time it is used after the file is opened.
Subsequent PUT commands will append to the file.
When the variable is a string, the number of bytes that will be written is equal to the dimensioned size of the string, and not how many bytes you write to the string. (this is in the help).
Appending.
If I un-comment the line "flength = filelen(myfilename)+1 " and change the next line to
Code:
put #128 , mytxt, flength
then each loop will append to the file, and not overwrite it. _________________ Neil
Last edited by njepsen on Fri Sep 18, 2015 9:59 am; edited 1 time in total
Did you check the table in the 'AVR-DOS File System' topic? I mean : 'Validity of the file I/O operations regarding the opening modes'
Some statements are only intended to work with certain modes. put and get will only work in binary mode.
they might work in other modes too, but the results may be unexpected.
AVR-DOS and the help were written by Josef a long time ago. At that time, we assumed that all bascom users would be familiar with QB/VB file handling. As a result the help covers the basic description. I will have a look at that.
Append is typically used for text files where you use print.
The reason why PUT writes the complete string is that put/get can read data just by specifying the index. that would not be possible if all strings would have a different length. _________________ Mark
Hi mark. Actually that was a typo. My testing was done with open for binary . The prog will not run with append.
I have edited the post.
Cheers _________________ Neil
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You cannot download files in this forum