WriteFixedString() & ReadFixedString()
Author: Jare
Added: 21. elokuuta 2011 kello 18.13
Edited: 22. elokuuta 2011 kello 11.13
Category: Tiedostot
Description
Kirjoitus- ja lukufunktiot merkkijonoille joilla on kiinteä pituus ja joita ei haluta ympäröidä tiedostossa erikoismerkeillä (kuten WriteString-komento tekee).
Kirjoituksen yhteydessä voidaan automaattisesti varmistaa, että kirjoitettava merkkijono on täsmälleen oikean mittainen.
Lukuvaiheessa voidaan lukea joko tietty määrä merkkejä, tai kunnes tiedosto loppuu. Jälkimmäisellä tavalla voidaan lukea vaikka kokonainen tiedosto yhteen merkkijonomuuttujaan.
Kommenteissa on tarkempaa tietoa.
Code
Select all1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | //WriteFixedString()
//This funktion writes a string having a fixed length to a previously opened file. Unlike WriteString(),
//this function does not wrap the string within special characters that tells where the string starts and stops.
//The string can be read from the file using any text editor such as Notepad.
//
//PARAMETERS:
//file - Pointer to a file that is opened with OpenToWrite or OpenToEdit command
//stri$ - The string to be written.
//length - The fuction will truncate the string to this length.
// Leave unset if you wish to use the full length of the string.
//filler$ - If the string is shorter than specified by the length parameter, it will be filled using the filler$
// string so that it's length will be equal to the length parameter. This can be a single character or
// a string of characters. Obviously this won't have effect if you leave the length parameter undefined.
//
//RETURNS: The string as it was written to the file so you can check how it was modified.
Function WriteFixedString(file, stri$, length=-1, filler$=".,")
If length > -1 Then stri = Left(stri+String(filler,length), length)
For i = 1 To Len(stri)
WriteByte file, Asc(Str(Mid(stri,i,1)))
Next i
Return stri
EndFunction
//ReadFixedString()
//This function reads a string having a fixed length that was written to a file using WriteFixedString() function.
//
//PARAMETERS:
//file - Pointer to a file that is opened with OpenToRead or OpenToEdit command
//length - Length of the string to be read. If you leave it unset, the function will read the file until it ends.
// BE EXTREMELY CAREFUL with this feature if you are dealing with large files!
// NOTICE: In a case that the file ends before the string was read up to it's desired length, the function
// returns the string as it is. If the length must be accurate, you should always check if the string is
// shorter than expected.
Function ReadFixedString(file, length=-1)
result$ = ""
While Not EOF(file)
i+1
result = result + Chr(Int(ReadByte(file)))
If i >= length And length > -1 Then Exit
Wend
Return result
EndFunction
|
Comments
#17 Sent by: Jare, 22. elokuuta 2011 kello 11.14
Kiitos VesQ, korjasin. :)
Leave a comment
You must be logged in to comment.
#16 Sent by: VesQ, 21. elokuuta 2011 kello 21.04
"Obviously this will have effect if you leave the length parameter undefined"
Varmaankin "will" pitäisi olla "won't" ;)