Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 146
  • Last Modified:

saving varients to a file

Hello,
I have to save my project's variants to a file, so whenever I will open the file I will be able to see the variant's values ,
In other words I want to be able to write to a file.
Thank you for helping me…
                                         chen.
0
chen_meir
Asked:
chen_meir
1 Solution
 
Glen RichmondCustomer Reporting Programmer.Commented:
to read the file use somthing like this
----------------------------------------

Dim fh As Integer
fh = FreeFile()

    Open MyFilePathAndName For Input As #fh
        Line Input #fh, Value1
        Line Input #fh, Value2
        Line Input #fh, Value3
        Line Input #fh, Value4
        Line Input #fh, Value5
    Close #fh


to Write the file use
----------------------------------------
Dim fh As Integer
fh = FreeFile()
   
 Open MyFilePathAndName For Output As #fh
        Print #fh, Value1
        Print #fh, Value2
        Print #fh, Value3
        Print #fh, Value4
        Print #fh, Value5
    Close #fh

Good luck
0
 
ArkCommented:
Hi
Above sample is for strings
If you don't know format of variables, it's better use "Write" statement:

Dim n As Integer
n = FreeFile
     
 Open "c:\test.txt" For Output As #
      Write #n, "This is a string"
'output:  "This is a string"
      Write #n, 12345
'output: 12345
      Write #n, Now
'output #2000-04-08 10:40:30"
      Write #n, True
'output #True#
      Write #n, Null
'output #Null#
    Close #n

when you'll read this file using Input statement, all datas will read correctly.
Cheers
0
 
john_priceCommented:
Why not use the registry?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
wsh2Commented:
In both examples above.. each field is written as a record.. multiple writes and multiple reads to get. To put/get all 6 fields in one record:

<----- Code Begin ----->

Dim MyVar1 As String
Dim MyVar2 As Integer
Dim MyVar3 As Variant
Dim MyVar4 As Long
Dim MyVar5 As Boolean
Dim MyVar6 As Date

MyVar1 = "String"
MyVar2 = 99
MyVar3 = "Variant"
MyVar4 = -12345
MyVar5 = True
MyVar6 = Now

Dim intFile As Integer
intFile = FreeFile
     
' WRITE the File

Open "c:\MyPath\MyFile.txt" For Output As #intFile
Write #intFile, MyVar1; MyVar2; MyVar3; MyVar4; MyVar5; MyVar6
Close #intFile

MyVar1 = ""
MyVar2 = 0
MyVar3 = ""
MyVar4 = 0
MyVar5 = False
MyVar6 = #12/31/1999#

' READ The File
Open "c:\MyPath\MyFile.txt" For Input As #intFile
Input #intFile, MyVar1, MyVar2, MyVar3, MyVar4, MyVar5, MyVar6
Close #intFile

MsgBox (MyVar1 & " " & _
   MyVar2 & " " & _
   MyVar3 & " " & _
   MyVar4 & " " & _
   MyVar5 & " " & _
   MyVar6)

<----- Code End ----->
0
 
ArkCommented:
:-))))
Private Type MyVars
  MyVar1 As String
  MyVar2 As Integer
  MyVar3 As Variant
  MyVar4 As Long
  MyVar5 As Boolean
  MyVar6 As Date
End Type
Dim MV As MyVars


Private Sub Command1_Click()
MV.MyVar1 = "String"
MV.MyVar2 = 99
MV.MyVar3 = "Variant"
MV.MyVar4 = -12345
MV.MyVar5 = True
MV.MyVar6 = Now

Open "c:\testfile" For Random As #1 Len = Len(MV)
Put #1, , MV
Close #1
End Sub

Private Sub Command2_Click()
Open "c:\testfile" For Random As #1 Len = Len(MV)
Get #1, , MV
Close #1
MsgBox MV.MyVar1 & MV.MyVar2 & MV.MyVar3 & MV.MyVar4 & MV.MyVar5 & MV.MyVar6
End Sub
'vb.hlp - rulezzz :-)
Cheers
0
 
wsh2Commented:
Ark:
Random access requires a FIXED record layout / length. The String we can fix, but the Variant is definitely a no no.. <smile>.

Change your Data Type to Read..

Private Type MyVars
  MyVar1 As String * 80
  MyVar2 As Integer
  MyVar3 As Variant <-- Can't use for random
  MyVar4 As Long
  MyVar5 As Boolean
  MyVar6 As Date
End Type

0
 
ArkCommented:
Nope
Wsh2, did you try code?
Strings don't need be fixed length - VB put 2 bytes before string - it's length (of course, you can't read this file as text). About variants -
MV.MyVar3 = "Variant" -works OK
MV.MyVar3 = 1234567 -works OK
MV.MyVar3 = True -works OK
MV.MyVar3 = Now -works OK
MV.MyVar3 = Null -works OK
MV.MyVar3 = Picture1.Picture - still works OK (write handle)
MV.MyVar3 = Array (1,2,3,4,5,6) :-(

Cheers
0
 
ArkCommented:
Wsh2,
Sorry, now agree - strings can be only up to 10 chars length. But we can save for binary.

Private Type MyVars
  MyVar1 As String
  MyVar2 As Integer
  MyVar3 As Variant
  MyVar4 As Long
  MyVar5 As Boolean
  MyVar6 As Date
End Type
Dim MV As MyVars


Private Sub Command1_Click()
'works!!
MV.MyVar1 = "1234567890123456789"
MV.MyVar2 = 99
'works!!
MV.MyVar3 = Array("zero", "one", "two", "three", "four")
MV.MyVar4 = -12345
MV.MyVar5 = True
MV.MyVar6 = Now

Open "c:\testfile" For Binary As #1
Put #1, , MV
Close #1
End Sub

Private Sub Command2_Click()
Open "c:\testfile" For Binary As #1
Get #1, , MV
Close #1
MsgBox MV.MyVar1 & MV.MyVar2 & MV.MyVar3(2) & MV.MyVar4 & MV.MyVar5 & MV.MyVar6
End Sub

Cheers
0
 
wsh2Commented:
Ark:
Here is the problem..

Open "c:\testfile" For Random As #1 Len = Len(MV)
Get #1, , MV

When you close the program and then start it again, when you go to open the file, how will the program open statement know the length of MV? It is going to use the empty MV type size, and due to that, a data underrun could easily take place.

Now, I could be wrong, maybe it will size properly on the fly. But I wouldn't want to bet the house on that.
As such, I never use variant data types for Disk I/O.. so in that regard, I hardly qualify as an expert.. <smile>.
0
 
wsh2Commented:
Ark:
Same problem applies for Binary, we don't know the size of MV before we read it.. <sigh>.
0
 
ArkCommented:
Private Sub Command2_Click()
MV.MyVar1 = 0
MV.MyVar2 = 0
MV.MyVar3 = 0
MV.MyVar4 = 0
MV.MyVar5 = 0
MV.MyVar6 = 0

Open "c:\testfile" For Binary As #1
Get #1, , MV
Close #1
MsgBox MV.MyVar1 & MV.MyVar2 & MV.MyVar3(2) & MV.MyVar4 & MV.MyVar5 & MV.MyVar6
End Sub

works OK. We don't need to know size of MV, only structure. Put statement place variable description (and size) for every variable in structure into file before this variable. Get statement read this description and fill variables properly.
Cheers
0
 
chen_meirAuthor Commented:
ark ,
thank you for the answers...
i will try your code and i hope it will work.
:-)
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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