Link to home
Start Free TrialLog in
Avatar of atlantiscd
atlantiscd

asked on

file to array -> change array -> array to file

textfile(location c:\test.txt) contents:
[Test1]
parity=none
option2=differerent

[Test2]
parity=odd
inverse
option2=differerent

[Test3]
parity=even
direct
option2=differerent

code:
----
Private Sub Form_Load()
Dim insideTest2 As Boolean
Open "c:\test.txt" For Binary As #1
  Dim strBuff As String: strBuff = Space(LOF(1))
  Get #1, , strBuff
  Close #1
Dim strArr() As String: strArr = Split(strBuff, vbCrLf)
Dim i As Long
 For i = 0 To UBound(strArr)
If RTrim$(strArr(i)) = "[Test2]" Then
 insideTest2 = True
Else
If Left(strArr(i), 1) = "[" And Right(RTrim(strArr(i)), 1) = "]" Then
insideTest2 = False
End If
End If
  If insideTest2 Then
    If Not InStr(strArr(i), "parity=") = 0 Then
Select Case RTrim$(LCase$(Mid$(strArr(i), InStr(strArr(i), "parity=") + 7)))
Case "none"
If strArr(i + 1) = "inverse" Then
'here I want to delete item from array
End If
If strArr(i + 1) = "direct" Then
'here I want to delete item from array
End If
Case "even"
If strArr(i + 1) = "direct" Then
Else
If strArr(i + 1) = "inverse" Then
ReDim Preserve strArr(UBound(strArr) + 1)
strArr(i + 1) = "direct"
Else
strArr(i) = strArr(i) & vbCrLf & "direct"
End If
End If
Case "odd"
If strArr(i + 1) = "inverse" Then
Else
If strArr(i + 1) = "direct" Then
ReDim Preserve strArr(UBound(strArr) + 1)
strArr(i + 1) = "inverse"
Else
strArr(i) = strArr(i) & vbCrLf & "inverse"
End If
End If
End Select
End If
End If
Next
Open "c:\test.txt" For Binary As #1
Put #1, , Join(strArr, vbCrLf)
Close #1
End Sub
---------------------
What I want to do:
1.where it says "here I want to delete item from array" , I want to do just that.
2. Make code more simple but do same things.
like if you change parity=none [Test2] in test.txt
[Test2] should look like [Test1]. But if you change
[Test2] parity=odd the text inverse should be there. And
[Test2] parity=even the text direct should be there.

(3.)Make it unixformat(I can but I can not post it here)
So this one is already solved.
Do this to make your created test.txt:
---
Open "c:\test.txt" For Binary As #1
Put #1, , Join(strArr, Chr(13))
Close #1
and then run project
stop project.
replace all "vbCrLf" with "Chr(13)"
now it is unixformat.
Avatar of Da_Weasel
Da_Weasel
Flag of United States of America image

looks like a .ini file to me, you can use the WritePrivateProfileString API.  I made a little class to make things easier for myself, here is the code.

Put this in a Module:
Option Explicit

Declare Function WritePrivateProfileString& Lib "kernel32" Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As String, ByVal lpFileName As String)
Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Put this in a Class Module(called clsINI):
Option Explicit
Private mIniFileName As String

Public Property Let FileName(ByVal INIFileName As String)
'On Error Resume Next
    If Dir(INIFileName, vbNormal) = "" Then
      Open INIFileName For Output As #1
      Close #1
    End If
    mIniFileName = INIFileName
End Property


Public Property Get FileName() As String
'On Error Resume Next
    FileName = mIniFileName
End Property



Public Function GetValue(ByVal Section As String, ByVal Key As String, Optional ByVal DefaultValue As String) As String
  On Error GoTo Hell
  Dim Value As String, retval As String, x As Integer
  retval = String$(255, 0)
  x = GetPrivateProfileString(Section, Key, DefaultValue, retval, Len(retval), mIniFileName)
  GetValue = Trim(Left(retval, x))
Exit Function
Hell:
  GetValue = DefaultValue
End Function

Public Function WriteValue(ByVal Section As String, ByVal Key As String, ByVal Value As String) As Boolean
  On Error GoTo Hell
  Dim x As Integer
  x = WritePrivateProfileString(Section, Key, Value, mIniFileName)
  If x <> 0 Then WriteValue = True
  Exit Function
Hell:
End Function

Public Function GetAllSections() As Collection
  Dim Value As String, retval As String, x As Integer
  Dim s() As String, i As Integer
  retval = String$(255, 0)
  x = GetPrivateProfileString(vbNullString, "", "", retval, Len(retval), mIniFileName)
  Value = Trim(Left(retval, x))
  s = Split(Value, Chr(0))
  Set GetAllSections = New Collection
  With GetAllSections
    For i = LBound(s) To UBound(s)
      If s(i) <> "" Then .Add s(i)
    Next
  End With
End Function

Public Function GetAllKeys(ByVal Section As String) As Collection
  Dim Value As String, retval As String, x As Integer
  Dim s() As String, i As Integer
  retval = String$(255, 0)
  x = GetPrivateProfileString(Section, vbNullString, "", retval, Len(retval), mIniFileName)
  Value = Trim(Left(retval, x))
  s = Split(Value, Chr(0))
  Set GetAllKeys = New Collection
  With GetAllKeys
    For i = LBound(s) To UBound(s)
      If s(i) <> "" Then .Add s(i)
    Next
  End With
End Function

Public Function DeleteSection(ByVal Section As String) As Boolean
  On Error GoTo Hell
  Dim x As Integer
  x = WritePrivateProfileString(Section, vbNullString, "", mIniFileName)
  If x <> 0 Then DeleteSection = True
  Exit Function
Hell:
End Function

Public Function DeleteKey(ByVal Section As String, ByVal Key As String) As Boolean
  On Error GoTo Hell
  Dim x As Integer
  x = WritePrivateProfileString(Section, Key, vbNullString, mIniFileName)
  If x <> 0 Then DeleteKey = True
  Exit Function
Hell:
End Function

This is how you would use it:
Dim MyConfig as new clsINI
MyConfig.FileName = "yourfile.ini"
MyConfig.WriteValue("Test1","parity","none")
If the section does not exist then it will create it.
MyString = MyConfig.GetValue("Test1","parity","not found")
this gets the value of a key. the last parameter is the default value, it gets returned if no value was found.
You can play around with it to see what the other Methods do.
Avatar of atlantiscd
atlantiscd

ASKER

hmm... Looks like you have problem to write the values.
inverse
direct
how do you get them at line after parity=odd/even/none?

thanks for response..
hmm... Looks like you have problem to write the values.
inverse
direct
how do you get them at line after parity=odd/even/none?

thanks for response..
ASKER CERTIFIED SOLUTION
Avatar of Da_Weasel
Da_Weasel
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
No it is NOT possible to do so.
Cause I use my app as a GUI for another app.
If I change it does not work in that app.
atlantiscd:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Experts: Post your closing recommendations!  Who deserves points here?