• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 205
  • Last Modified:

Saving Files

I have this program where it calculates the stats and stuff for Dungeons and Dragons.  First you put in you race and class and then it calculates your str, dex, con, int, wis, and cha by random nuber generation and modifiers by your race and class.  I want it so it will save everything how it is.  I want it to save in a *.clc file.  Can anyone give me sample code?
1 Solution
I think what you are after is pretty easy, but I don't quite understand your lingo.........I am not farmiliar with D+D, but I think your problem can be easily solved.

a random number will be generated between 0 and 1, so you can get integer numbers in a range you want by doing.


inCon = int(rnd*NumVariables+1)

NumVariables is how ever high you want the random numbers to go.

Let me know what you want to do with these numbers.
HI Alien,

use INI format to save the characters... of course you can call the file *.clc as you want...

for the code...

to read the variable

cHealth = ReadINI("Stats", "Character_Health", CLCFilePath)

to write.. use

WriteINI "Stats", "Character", cHealth, CLCFilePath

in a module file.. add this lines..

' declaration for read/write ini
Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

' Procedure : ReadINI
' Author    : rana
' Purpose   : reads an ini file
Public Function ReadINI(ByVal strsection As String, ByVal strKey As String, ByVal strfullpath As String) As String
   Dim strBuffer As String
   Let strBuffer$ = String$(750, Chr$(0&))
   Let ReadINI$ = Left$(strBuffer$, GetPrivateProfileString(strsection$, ByVal LCase$(strKey$), "", strBuffer, Len(strBuffer), strfullpath$))
End Function
' Procedure : WriteINI
' Author    : rana
' Purpose   : write to an ini file
Public Sub WriteINI(ByVal strsection As String, ByVal strKey As String, ByVal strkeyvalue As String, ByVal strfullpath As String)
    Call WritePrivateProfileString(strsection$, UCase$(strKey$), strkeyvalue$, strfullpath$)
End Sub

Try this, if oyu'd like. I've odne osmething similar. The names of controls in this code shoudl be pretty self explanatory. I AM a HUGE D&D fan, so I guess I can give ya a little something...comments included to help ya along...
Private Sub menuSave_Click()
'You'll need a Common Dialog control on your
'form...mine here is named "dlgMain". May have
'to add it from Project>>Components
'Call Save Dialog
    dlgMain.Flags = cdlOFNOverwritePrompt
    dlgMain.InitDir = App.Path & "\stats"
    dlgMain.FileName = ""
    dlgMain.Filter = "Stat Files (*.clc)|*.code|All Files (*.*)|*.*"
    dlgMain.DialogTitle = "Save Stats As..."
    If Len(dlgMain.FileName) = 0 Then Exit Sub
'End Save Dialog Statistics
Open dlgMain.FileName For Output As #1
'The format of the file owudl be as follows...
'For more stats, such as adding a name, add it where needed
Print #1, txtRace.Text & vbCrLf & txtClass.Text & vbCrLf & txtStr.Text & vbCrLf & txtDex.Text & vbCrLf & txtCon.Text & vbCrLf & txtWis.Text & vbCrLf & txtInt.Text & vbCrLf & txtCha.Text
Close #1
End Sub
End Sub
'The drawback to this is that each stat (*.clc) file
'must be saved individually, I.E. Your character can't be
'in the same file as oyur friend's, they're two different
'ones. If youy need a isngle writeable file, feel free to
'e-mail me, and I'd be happy to get you the code you need '
for it.
this works; lets say you have all the values you want and you have a save and load button:

' save the file
Private Sub cmdSave_Click()
Dim intFile As Integer
    intFile = FreeFile

    ' "c:\MyHeroStats.clc" is just an example; you should use a different file name for every character. i recommend using common dialog control

    Open "c:\MyHeroStats.clc" For Output As intFile
    Print #intFile, Trim(txtClass.Text)
    Print #intFile, Trim(txtDex.Text)
    Close intFile

End Sub

--- this will save the file as a .clc format; now for loading, do this

' load file
Private Sub cmdLoad_Click()
Dim intFile As Integer
Dim strClass As String
Dim strDexterity As String

    intFile = FreeFile

    ' specify the name of the character you want to load; again i recommend common dialog control    

    Open "c:\MyHeroStats.clc" For Input As intFile
    Input #intFile, strClass
    Input #intFile, strDexterity
    txtClass.Text = strClass
    txtDex.Text = strDexterity
    Close intFile

End Sub

' i just included two fields (class and dex) but u could add more

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now