Saving an array to textfile [solved]

Posts 1–14 of 14 · Page 1 of 1
Saving an array to textfile [solved]
Hello, How do i save a array? Can i use Application.Settings?
I want to write it to a file (txt) but the array its a structure..
And i ill have problems reading it...
What should i do? I wont do splits cuz a part of the structure is a string that can have more then one line..
Help me =/

I want to save and read the array (that is a structure with strings that can have more then one line)

Thanks

@Jason
@Blubb1337
@NextGen1
@Hassan
(Dont remember the other pros name )
String manipulation. Create a file structure like:

StructureName='Struct Name'
Struct Start:
Variable1 = "Hello World" + Newline + "This is MGPH"
Variable2 = "Hello World" + Newline + "This is Visual Basic Section"
Struct End:

Something like this. Store it like this and parse it on read. When storing replace Newlines with something short you can later identify and convert back to newline.

That's how I'd do it. There may be a better solution. ;]
Show a sample array you need to save.
@Hassan ill try something like that..

@Blubb1337

I got a structure like this:

Code:
 Structure AccInfo
        Dim Login As String
        Dim Password As String
        Dim Details As String
    End Structure
Then i declared a Array() As AccInfo...
And each array have they info.. Like Array.Login Array.Password Array.Details..
And the Details can contain more then one line...

but im having troble while saving and reading...(Idk how to save to be easy to read..)
@Lyoto Machida

[highlight="VB.NET"]#Region "Declaration"

Dim Accs As List(Of AccInfo)

Structure AccInfo
Dim Login As String
Dim Password As String
Dim Details As List(Of String)
End Structure

#End Region

#Region "Functions"

Private Sub Savelist(ByVal Where2Save As String, ByVal Accounts As List(Of AccInfo), Optional ByVal Delimiter As Char = "|"c)
Dim strb As New System.Text.StringBuilder

Dim details As String = String.Empty

For i = 0 To Accounts.Count - 1
Dim acc As AccInfo = Accounts(i)

With acc
For det = 0 To .Details.Count - 1
details &= String.Concat(.Details(det), ";")
details = details.Substring(0, -1)
Next
strb.AppendLine(String.Concat(.Login, Delimiter, .Password, Delimiter, details))
End With
Next

Using sWrite As New IO.StreamWriter(Where2Save)
sWrite.Write(strb.ToString)
End Using
End Sub

Private Function RetreiveList(ByVal Path2Read As String, Optional ByVal Delimiter As String = "|"c) As List(Of AccInfo)
Dim Content As String = IO.File.ReadAllText(Path2Read)

Dim Accounts As New List(Of AccInfo)

For Each lines As String In Content.Split(vbCrLf.ToCharArray)
Dim splitted() As String = lines.Split(Delimiter)

For i = 0 To splitted.Count - 1
Dim acc As New AccInfo

Dim splitdetails() As String = splitted(2).Split(";")

Dim details As New List(Of String)

For det = 0 To splitdetails.Count - 1
details.Add(splitdetails(det))
Next

With acc
.Login = splitted(0)
.Password = splitted(1)
.Details = details
End With

Accounts.Add(acc)
Next
Next

Return Accounts
End Function

#End Region[/highlight]

Quickly wrote this up for you.

I changed the structure a bit.

Btw, I have not tested this at ALL.

Add me on msn if you have further questions...take a look at my profile if you don't have me yet...
Quote Originally Posted by Blubb1337 View Post
@Lyoto Machida

[highlight="VB.NET"]#Region "Declaration"

Dim Accs As List(Of AccInfo)

Structure AccInfo
Dim Login As String
Dim Password As String
Dim Details As List(Of String)
End Structure

#End Region

#Region "Functions"

Private Sub Savelist(ByVal Where2Save As String, ByVal Accounts As List(Of AccInfo), Optional ByVal Delimiter As Char = "|"c)
Dim strb As New System.Text.StringBuilder

Dim details As String = String.Empty

For i = 0 To Accounts.Count - 1
Dim acc As AccInfo = Accounts(i)

With acc
For det = 0 To .Details.Count - 1
details &= String.Concat(.Details(det), ";")
details = details.Substring(0, -1)
Next
strb.AppendLine(String.Concat(.Login, Delimiter, .Password, Delimiter, details))
End With
Next

Using sWrite As New IO.StreamWriter(Where2Save)
sWrite.Write(strb.ToString)
End Using
End Sub

Private Function RetreiveList(ByVal Path2Read As String, Optional ByVal Delimiter As String = "|"c) As List(Of AccInfo)
Dim Content As String = IO.File.ReadAllText(Path2Read)

Dim Accounts As New List(Of AccInfo)

For Each lines As String In Content.Split(vbCrLf.ToCharArray)
Dim splitted() As String = lines.Split(Delimiter)

For i = 0 To splitted.Count - 1
Dim acc As New AccInfo

Dim splitdetails() As String = splitted(2).Split(";")

Dim details As New List(Of String)

For det = 0 To splitdetails.Count - 1
details.Add(splitdetails(det))
Next

With acc
.Login = splitted(0)
.Password = splitted(1)
.Details = details
End With

Accounts.Add(acc)
Next
Next

Return Accounts
End Function

#End Region[/highlight]

Quickly wrote this up for you.

I changed the structure a bit.

Btw, I have not tested this at ALL.
Thanks alot ill test it in 20 min then i give some feedback =)

@Blubb1337
Now details its a list or something ,how i change the value? like AccInfo.Details = "The Acc details" ?
Dim list as new list(of string)

list.add("info")

accinfo.details(list)
Quote Originally Posted by Blubb1337 View Post
Dim list as new list(of string)

list.add("info")

accinfo.details(list)
hmm Thanks =)
Making some changes and ill test it soon

EDIT: Made some changes, Its working =)
Thanks dude +rep!

@Jason /solved
Glad you got it working :]
Parserboi will come up with a neater solution, but marked solved in the meantime

EDIT: some code, (note, do not have an IDE here so this was written off the top of my head, so if there's any errors...get off me )

[highlight=vb.net]
Private Sub SaveShit(ByVal yourList As List(Of AccInfo), ByVal saveLoc As String)
Dim inputArr As AccInfo() = yourList.ToArray
Dim saveStr As String = String.Join(vbCr, Array.ConvertAll(inputArr, Function(a As AccInfo) String.Concat(a.login, "~", a.password, "~", String.Join("|"c, a.details.ToArray))))
IO.File.WriteAllText(saveLoc, saveStr)
End Sub

Private Function RetrieveShit(ByVal whereToLook As String) As List(Of AccInfo)
Dim rawArray As String() = IO.File.ReadAllText(whereToLook).Split(vbCr)
Dim output As IEnumerable(Of AccInfo) = From line As String In rawArray Select New AccInfo With {.Login = line.Split(New Char() {"~"c})(0), .Password = line.Split(New Char() {"~"c})(1), .Details = line.Split(New Char() {"~"c})(2).Split(New Char() {"|"c}).ToList}
Return output.ToList
End Function
[/highlight]

Now if that works, I truly am a genious.
EDIT2: Put it into an IDE, found a few errors and fixed them
Quote Originally Posted by Jason View Post
Parserboi will come up with a neater solution, but marked solved in the meantime

EDIT: some code, (note, do not have an IDE here so this was written off the top of my head, so if there's any errors...get off me )

[highlight=vb.net]
Private Sub SaveShit(ByVal yourList As List(Of AccInfo), ByVal saveLoc As String, Optional ByVal delimiter As Char() = {Environment.NewLine})
Dim inputArr As AccInfo() = yourList.ToArray
Dim saveStr As String = String.Join(delimiter, Array.ConvertAll(inputArr, function(a As AccInfo) String.Concat(a.Login, "~", a.Password, "~", String.Join("|"c, a.Details.ToArray))))
IO.File.WriteAllText(saveLoc, saveStr)
End Sub

Public Function RetrieveShit(ByVal whereToLook, Optional ByVal delimiter As Char() = {Environment.NewLine}) As List(Of AccInfo)
Dim rawArray As String() = IO.File.ReadAllText(whereToLook).Split(delimiter)
Dim output As IEnumerable(Of AccInfo) = From line As String in rawArray Select New AccInfo With {.Login = line.split(New Char() {"~"c})(0), .Password = line.split(New Char() {"~"c})(1), .Details = line.split(New Char() {"~"c}(2).Split(New Char() {"|"c}).ToList}
Return output.ToList
End Function
[/highlight]

Now if that works, I truly am a genious.
Shit ! truly are a pro in vb now !
Quote Originally Posted by Hassan View Post


Shit ! truly are a pro in vb now !
He's a better parser than you

As you guys know, I suck in parsing and such crap. But my solution works, so

Edit: Your code is not errorfree :P
Quote Originally Posted by Blubb1337 View Post
He's a better parser than you

As you guys know, I suck in parsing and such crap. But my solution works, so

Edit: Your code is not errorfree :P
I updated it and it's now error free I think?
Quote Originally Posted by Blubb1337 View Post
He's a better parser than you

As you guys know, I suck in parsing and such crap. But my solution works, so

Edit: Your code is not errorfree :P
Umm, he will be better than me when I can't parse and unable to provide solution. He seems better because of short code. Anyways, I needa learn Lambda expressions !!
Posts 1–14 of 14 · Page 1 of 1
This thread is closed for replies.

Tags for this Thread

None

Talk with us