EDIT: Be sure to check out the
IncodeFile and
DecodeFile functions! I believe them to be unhackable, well they are as far as my skills go
.
I stayed up the last couple of nights creating a set of functions
that make FIlE control much, much, much easier. There are a total
of 15 functions all together which allow you to have complete
control over writting, reading, and returning data in files. Also
keep in mind that these functions work in BOTH DBC and DBPRO, so
you won't have to worry about that because I've tested them in
both programs, and if you'd like to test it yourself then you can
very easily, since I've created a FULL DEMO that uses all 15
functions.
Here's a COMPLETE LIST of all 15 functions:
-PrintFileContentsToScreen(File$,Top)
-IncodeFile(File$,Seed,Overwrite)
-DecodeFile(File$,Seed,Overwrite)
-GetRidOfBlankLines(File$,Overwrite)
-GetRidOfAllSpacesInFile(File$,Overwrite)
-GetRidOfStringAtStart(File$)
-GetRidOfStringAtEnd(File$)
-GetRidOfStringAtPos(File$,Pos)
-WriteStringToStart(File$,Write$)
-WriteStringToEnd(File$,Write$)
-WriteStringToPos(File$,Write$,Pos,Overwrite)
-Return Varible = CountLinesInFile(File$,Rid)
-Return Varible = ReadStringAtStart(File$)
-Return Varible = ReadStringAtEnd(File$)
-Return Varible = ReadStringAtPos(File$,Pos)
Here's a DETAILED DESCRIPTION and SYNTAX of all 15 functions:
------------------------------------------
-PrintFileContentsToScreen(File$,Top)
This function allows you to print the complete contents of a specific file to the
screen. However, keep in mind that only so many lines can be visible on the
screen at one time. First type in the full name of the file whose contents you
want to be displayed to the screen. Then you MUST type in a value of either 0 or 1.
A 0 meaning that the Print will start where it left off, and a 1 meaning that the
Print will start at the top of the screen.
SYNTAX
PrintFileContentsToScreen(string,interger)
------------------------------------------
-IncodeFile(File$,Seed,Overwrite)
This function will make it so that users won't be able to read your file or decode it.
You first type in the full name of the file, then the SEED value. The seed value is a
number that you make up to incode your file with, BE SURE to keep this number secret.
A seed value of 0 will NOT incode the file, but completely erase everything in it, be
sure not to use a seed value of 0. A SAFE range of SEED values would be between -99999
and -1000, or between 1000 and 99999. Also keep in mind that the farther the seed
value is from 0, the more it may take to incode the file. Next you type in either a 0,
which saves the results of the file as a file named "IncodedFile.dat", or you can type
in a value of 1, which will overwrite the file that you are incoding.
SYNTAX
IncodeFile(string,interger,interger)
------------------------------------------
-DecodeFile(File$,Seed,Overwrite)
This function will allow you take a file that you've ALREADY incoded and translate it
back into its original version. HOWEVER you must know the correct SEED value that you
used to incode the file with or the results will NOT be even close to its original
version. So, first type in the full name of the file, and then its SEED value. Next
you type in either a 0, which saves the results of the file as a file named
"DecodedFile.dat", or you can type in a value of 1, which will overwrite the file that
you are decoding.
SYNTAX
DecodeFile(string,interger,interger)
------------------------------------------
-GetRidOfBlankLinesInFile(File$,Overwrite)
This function will allow you to get rid of ALL blank lines in a file. First type in
the full name of the file. Then you must type in either a 0, which saves the results
of the file as a file named "FileWithoutBlankLines.dat", or you can type in a value
of 1, which will overwrite the file.
SYNTAX
GetRidOfBlankLinesInFile(string,interger)
------------------------------------------
-GetRidOfAllSpacesInFile(File$,Overwrite)
This function will allow you to get rid of ALL spaces in a file, INCLUDING blank
lines First type in the full name of the file. Then you must type in either a 0,
which saves the results of the file as a file named "FileWithoutSpaces.dat", or
you can type in a value of 1, which will overwrite the file.
SYNTAX
GetRidOfAllSpacesInFile(string,interger)
------------------------------------------
-GetRidOfStringAtStart(File$)
This function will allow you to get rid of the FIRST string at the start of a
a specific file. Just type in the full name of the file.
SYNTAX
GetRidOfStringAtStart(string)
------------------------------------------
-GetRidOfStringAtEnd(File$)
This function will allow you to get rid of the LAST string at the end of a
specific file. Just type in the full name of the file.
SYNTAX
GetRidOfStringAtEnd(string)
------------------------------------------
-GetRidOfStringAtPos(File$,Pos)
This function will allow you to get rid of a string at a certain POSITION in
a specific file. First type in the full name of the file, then the position
of the string that you want to get rid of. The position SIMPLY means which
line number.
SYNTAX
GetRidOfStringAtPos(string,interger)
------------------------------------------
-WriteStringToStart(File$,Write$)
This function will allow you to write a string to the START of a file. First type in
the full name of the file, then the string you want to be written to the start of the
file.
SYNTAX
WriteStringToStart(string,string)
------------------------------------------
-WriteStringToEnd(File$,Write$)
This function will allow you to write a string to the END of a file. First type in
the full name of the file, then the string you want to be written to the end of the
file.
SYNTAX
WriteStringToEnd(string,string)
------------------------------------------
-WriteStringToPos(File$,Write$,Pos,Overwrite)
This function will allow you to write a string to a specific POSITION in a file.
First type in the full name of the file, and then the string you want to be written
to the file. Next you must type in the position you want the string to be written
to in the file, this SIMPLY means, which line number you want the string to be
written to in the file. Then you MUST type in a value of 0 or 1 for the Overwrite.
A 0 meaning that the string that was previously at the specified position will not
be over written, but pushed down to the next position in the file. And a 1 meaning
that the string that was previously at the specified position will be overwritten
by the new string.
SYNTAX
WriteStringToPos(string,string,interger,interger)
------------------------------------------
-Return Varible = CountLinesInFile(File$,Rid)
This function wiil allow you to RETURN the total number of strings in the specified
file. First type in the full name of the file. Then you MUST type in a value of
either 0 or 1 for the Rid. The Rid is used to compensate a BUG that is in both DBC
and DBPRO, which is when a file is created with the Open To Write command an extra
line is always created at the end of the file. Now this can cause many little errors,
like returning the incorrect amount of lines in a file because that extra blank line
at the end of the file that you did't intend to create will be counted as a line.
HOWEVER, the typing in a value of 1 for the Rid will allow that extra line to not be
counted in the total lines in the file. And a value of 0 will count that extra line
at the end of the file. Remember that this function returns a interger, so you MUST
act on that.
SYNTAX
interger varible = CountLinesInFile(string,interger)
------------------------------------------
-Return Varible = ReadStringAtStart(File$)
This function will allow you to RETURN the first string in a file. Just type in the
full name of the file, and the FIRST string in the file will be returned. Remember
this function returns a string, so you MUST act on that.
SYNTAX
string varible = ReadStringAtStart(string)
------------------------------------------
-Return Varible = ReadStringAtEnd(File$)
This function will allow you to RETURN the last string in a file. Just type in the
full name of the file, and the LAST string in the file will be returned. Remember
this function returns a string, so you MUST act on that.
SYNTAX
string varible = ReadStringAtEnd(string)
------------------------------------------
-Return Varible = ReadStringAtPos(File$,Pos)
This function will allow you to RETURN a string at a certain POSITION in a file.
First type in the full name of the file, then the position where you want to read
from. This SIMPLY means, which line number you want to read from. Remember that
this function returns a string, so you MUST act on that.
SYNTAX
string varible = ReadStringAtPos(string,interger)
------------------------------------------
Here's the 15 FUNCTION'S code:
Rem *** Include File: FileFunctions.dba ***
Rem Created: 8/13/2005 12:13:57 AM
Rem Created By: Underworld1020
Rem ***** You Can Use This File In Anyway You Want, Just Leave The Credits Be *****
Function PrintFileContentsToScreen(File$,Top)
If Top=1 Then Set Cursor 0,0
Open To Read 1,File$
While File End(1)=0
Read String 1,Data$
If Len(Data$)>0
Print Data$
Else
Print
EndIf
EndWhile
Close File 1
EndFunction
Function IncodeFile(File$,Seed,Overwrite)
Lines=CountLinesInFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$
If Overwrite=0
Output$="IncodedFile.dat"
If File Exist(Output$)=1 Then Delete File Output$
Open To Write 1,Output$
Else
Delete File File$ : Open To Write 1,File$
EndIf
Open To Read 2,Temp$
For LineNum= 1 To Lines
Read String 2,Data$
Write$=""
For Num= 1 To Len(Data$)
Value=Asc(Mid$(Data$,Num))*Seed
Wrap=0
While (Wrap*256)<Value
Inc Wrap
EndWhile
Dec Wrap
Remainder=(Value-(Wrap*256))
Write$=Write$+Str$(Remainder)+"["+Str$(Wrap)+"]"
Next Num
Write String 1,Write$
Next LineNum
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function DecodeFile(File$,Seed,Overwrite)
Lines=CountLinesInFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$
If Overwrite=0
Output$="DecodedFile.dat"
If File Exist(Output$)=1 Then Delete File Output$
Open To Write 1,Output$
Else
Delete File File$ : Open To Write 1,File$
EndIf
Open To Read 2,Temp$
For LineNum= 1 To Lines
Read String 2,Data$
Write$=""
Chars=Len(Data$)
Num=0
While Num<Chars
Char$=""
While Mid$(Data$,Num)<>"["
Inc Num
Char$=Char$+Mid$(Data$,Num)
EndWhile
Char$=Left$(Char$,Num-1)
Remainder=Val(Char$)
Char$=""
Repeat
Inc Num
Char$=Char$+Mid$(Data$,Num)
Until Mid$(Data$,Num)="]"
Char$=Left$(Char$,Num-1)
Wrap=Val(Char$)
Value=((Wrap*256)+Remainder)/Seed
Write$=Write$+Chr$(Value)
EndWhile
Write String 1,Write$
Next LineNum
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function GetRidOfBlankLinesInFile(File$,Overwrite)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$
If Overwrite=0
Output$="FileWithoutBlankLines.dat"
If File Exist(Output$)=1 Then Delete File Output$
Open To Write 1,Output$
Else
Delete File File$ : Open To Write 1,File$
EndIf
Open To Read 2,Temp$
While File End(2)=0
Read String 2,Data$
For Num= 1 To Len(Data$)
If Asc(Mid$(Data$,Num))<>32 Then Write String 1,Data$ : Exit
Next Num
EndWhile
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function GetRidOfAllSpacesInFile(File$,Overwrite)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$
If Overwrite=0
Output$="FileWithoutSpaces.dat"
If File Exist(Output$)=1 Then Delete File Output$
Open To Write 1,Output$
Else
Delete File File$ : Open To Write 1,File$
EndIf
Open To Read 2,Temp$
While File End(2)=0
Read String 2,Data$
Write$=""
For Num= 1 To Len(Data$)
If Asc(Mid$(Data$,Num))<>32 Then Write$=(Write$+Mid$(Data$,Num))
Next Num
If Len(Write$)>0 Then Write String 1,Write$
EndWhile
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function GetRidOfStringAtStart(File$)
Lines=CountLinesInFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$ : Delete File File$
Open To Write 1,File$ : Open To Read 2,Temp$
Read String 2,Data$
For LineNum= 1 To (Lines-1)
Read String 2,Data$ : Write String 1,Data$
Next LineNum
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function GetRidOfStringAtEnd(File$)
Lines=CountLinesInFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$ : Delete File File$
Open To Write 1,File$ : Open To Read 2,Temp$
For Num= 1 To (Lines-1)
Read String 2,Data$ : Write String 1,Data$
Next Num
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function GetRidOfStringAtPos(File$,Pos)
Lines=CountLinesinFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$ : Delete File File$
Open To Write 1,File$ : Open To Read 2,Temp$
For Num= 1 To (Pos-1)
Read String 2,Data$ : Write String 1,Data$
Next Num
Read String 2,Data$
For LineNum= (Pos+1) To Lines
Read String 2,Data$ : Write String 1,Data$
Next LineNum
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function WriteStringToStart(File$,Write$)
Lines=CountLinesInFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$ : Delete File File$
Open To Write 1,File$ : Open To Read 2,Temp$
Write String 1,Write$
For LineNum= 1 To Lines
Read String 2,Data$ : Write String 1,Data$
Next LineNum
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function WriteStringToEnd(File$,Write$)
Lines=CountLinesInFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$ : Delete File File$
Open To Write 1,File$ : Open To Read 2,Temp$
For LineNum= 1 To Lines
Read String 2,Data$ : Write String 1,Data$
Next LineNum
Write String 1,Write$
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function WriteStringToPos(File$,Write$,Pos,Overwrite)
Lines=CountLinesInFile(File$,1)
Temp$="Temp.dat"
If File Exist(Temp$)=1 Then Delete File Temp$
Copy File File$,Temp$ : Delete File File$
Open To Write 1,File$ : Open To Read 2,Temp$
For PosNum= 1 To (Pos-1)
Read String 2,Data$ : Write String 1,Data$
Next PosNum
Write String 1,Write$
If OverWrite=1 Then Read String 2,Data$ : Inc Pos
For LineNum= Pos To Lines
Read String 2,Data$ : Write String 1,Data$
Next LineNum
Close File 1 : Close File 2
Delete File Temp$
EndFunction
Function CountLinesInFile(File$,Rid)
Count=0
Open To Read 1,File$
While File End(1)=0
Inc Count : Read String 1,Data$
EndWhile
Close File 1
If Rid=1 Then Dec Count
EndFunction Count
Function ReadStringAtStart(File$)
Open To Read 1,File$
Read String 1,Read$
Close File 1
EndFunction Read$
Function ReadStringAtEnd(File$)
Open To Read 1,File$
While File End(1)=0
Read String 1,Data$ : If Len(Data$)>0 Then Read$=Data$
EndWhile
Close File 1
EndFunction Read$
Function ReadStringAtPos(File$,Pos)
Open To Read 1,File$
For PosNum= 1 To Pos
Read String 1,Read$
Next PosNum
Close File 1
EndFunction Read$
Here's the DEMO code:
Rem Project: FileFunctions DEMO
Rem Created: 7/12/2005 7:03:06 PM
Rem Created By: Underworld1020
Rem ***** Main Source File *****
#Include "FileFunctions.dba"
Set Window On : GoSub Create_TestFile
Print "Since This Demo Is Pretty Long I've Decided To Let"
Print "You Have Control Of The Message Speed" : Print
Print "1-Slow, 2-Normal, 3-Fast" : Print
Input "Select Speed: ",Delay : Cls
If Delay<1 Or Delay>3 Then Delay=6000
If Delay=1 Then Delay=7000
If Delay=2 Then Delay=6000
If Delay=3 Then Delay=5000
Print "Original File:" : Print
Lines=CountLinesInFile(File$,1) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "There Are A Toal Of "+Str$(Lines)+" Lines In The File"
Wait Delay : Cls
Print "File After Incoded With A Seed Value Of 100" : Print
IncodeFile(File$,100,1) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That The File Is Now Unreadable"
Wait Delay : Cls
Print "File After Decoding With A INCORRECT Seed Value Of 50" : Print
DecodeFile(File$,50,0) : PrintFileContentsToScreen("DecodedFile.dat",0)
Wait Delay : Print : Print "Notice The File Will NOT Decode Correctly Without The Correct SEED value"
Wait Delay : Cls : Delete File "DecodedFile.dat"
Print "File After Decoding With A CORRECT seed value of 100" : Print
DecodeFile(File$,100,1) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice The File Is Now Correctly Decoded When The Correct SEED Value Is Used"
Wait Delay : Cls
Print "File After Taking Out All The Blank Lines" : Print
GetRidOfBlankLinesInFile(File$,0) : PrintFileContentsToScreen("FileWithoutBlankLines.dat",0)
Wait Delay : Print : Print "Notice All The Blank Lines Are Gone"
Wait Delay : Cls : Delete File "FileWithoutBlankLines.dat"
Print "File After Taking Out All Spaces" : Print
GetRidOfAllSpacesInFile(File$,0) : PrintFileContentsToScreen("FileWithoutSpaces.dat",0)
Wait Delay : Print : Print "Notice That ALL Spaces Are Gone, Including Blank Lines"
Wait Delay : Cls : Delete File "FileWithoutSpaces.dat"
Print "File After Writing A New String To The Start Of The File" : Print
WriteStringToStart(File$,"I'm New Here!!!") : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That A New String Has Easily Been Written To The Start Of The File"
Wait Delay : Cls
Print "File After Taking Out The First String At The Start Of The File" : Print
GetRidOfStringAtStart(File$) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That The New String At The Start Of The File Has Been Easily Removed"
Wait Delay : Cls
Print "File After Writing A New String To The End Of The File" : Print
WriteStringToEnd(File$,"I'm New Here!!!") : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That A New String Has Easily Been Written To The End Of The File"
Wait Delay : Cls
Print "File After Taking Out The Last String At The End Of The File" : Print
GetRidOfStringAtEnd(File$) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That The New String At The End Of The File Has Been Easily Removed"
Wait Delay : Cls
Print "File After Writing A New String To The 3rd Line In The File" : Print
WriteStringToPos(File$,"I'm New Here!!!",3,1) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That A New String Has Easily Been Written To The 3rd Line In The File"
Wait Delay : Print : Print "And Also That The 3rd Line Was Overwritten By The New String"
Wait Delay : Print : Print "This Is Because A Value Of 1 Was Used For The Overwrite To Make This Happen"
Wait Delay : Cls
Print "Now Lets Use The Same Function, But Use A Value Of 0 For The Overwrite" : Print
WriteStringToPos(File$,"I'm number 3",3,0) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That The New String Was Written To The 3rd Line In The File"
Wait Delay : Print : Print "And Also Notice That This Time The Old String On The 3rd Line In The File"
Print "Was Pushed Down And Not Overwritten"
Wait Delay : Print : Print "This Is Because A Value Of 0 Was Used For The Overwrite To Make This Happen"
Wait Delay : Cls
Print "File After Taking Out The String On The 4th Line In The File" : Print
GetRidOfStringAtPos(File$,4) : PrintFileContentsToScreen(File$,0)
Wait Delay : Print : Print "Notice That The String On The 4th Line Of The File Has Easily Been Removed"
Wait Delay : Cls
Print "The First String At The Start Of The File" : Print
Return$=ReadStringAtStart(File$) : Print Return$
Wait Delay : Print : Print "Notice That The First String At The Start Of The File Was Easily Returned"
Wait Delay : Cls
Print "The Last String At The End Of The File" : Print
Return$=ReadStringAtEnd(File$) : Print Return$
Wait Delay : Print : Print "Notice That The Last String At The End Of The File Was Easily Returned"
Wait Delay : Cls
Print "The String On The 2nd Line Of The File" : Print
Return$=ReadStringAtPos(File$,2) : Print Return$
Wait Delay : Print : Print "Notice That The String On The 2nd Line Of The File Was Easily Returned"
Wait Delay : Cls
Delete File File$
Center Text 320,240,"Created By: Underworld1020"
Wait Delay : End
Create_TestFile:
File$="Test.dat"
If File Exist(File$)=1 Then Delete File File$
Open To Write 1,File$
Write String 1,"This is line 1"
Write String 1,"Hey #2"
Write String 1,"I'm number 3"
Write String 1,""
Write String 1,"I'm 4"
Write String 1,"I'm 5!"
Close File 1
Return
Now BE SURE to save the FUNCTIONS as a .dba file called FileFunctions,
and be sure to KEEP THAT FILE IN SAME DIRECTORY AS THE FILE THAT WILL USE
THE FUNCTIONS. Also MAKE SURE THAT YOU USE THE #INCLUDE command to include
the FileFunctions.dba file with your MAIN SCOURCE file.
I've also included a .zip file with this post that includes the
following in it:
-FileFunctions - Descriptions.txt
-FileFunctions - List.txt
-FileFunctions DEMO.dba
-FileFunctions.dba
-Thread Post.txt
If you just get the .zip file and open it up you can just run the
FileFunctions DEMO.dba in either DBC or DBPRO and test it out. Now I've
tested all 15 of these functions quite a bit, and I'm pretty sure that
I've fixed all the bugs, but if you do happen to find a bug, then please
report it to me as soon as possible. ENJOY!