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

Deleting Info From Write File

Im working on an IRC program, Im trying now to save the servers, so the user wont have to remember them all
I have a list box, I put inside it the server names that are saved using this code
Text1 = Server
Text2 = Port
[CODE]
Dim Ser(2500) As String
Dim Por(2500) As String
Dim TotalWords As Integer

Private Sub Command6_Click()
'How To Save
Ser(TotalWords) = Text1.Text
Por(TotalWords) = Text2.Text
Open "Servers.dat" For Append As #1
    Write #1, Ser(TotalWords), Por(TotalWords)
Close
TotalWords = TotalWords + 1

'How To Load
List1.Clear
TotalWords = 0
Open "Servers.dat" For Input As #1
    Do Until EOF(1) = True
        Input #1, Ser(TotalWords), Por(TotalWords)
        TotalWords = TotalWords + 1
    Loop
Close
Dim x As Integer
For x = 0 To TotalWords - 1
    List1.AddItem Ser(x)
    DoEvents
Next x
End Sub
[/CODE]

Ok the problem is, I want to make the user able to remove some servers by highlighting them on the list and clicking command button, but then I have to remove them from the file it self and I must not keep any blank lines or spaces in the file (need that for another thing)
So how to do that?
0
deathman5
Asked:
deathman5
4 Solutions
 
sgayatriCommented:
The logic yu have to use is :
1. Whenever an item in the list box is clicked,
and delete button is pressed,
save the listindex of the item clicked.
2. delete that item from the list
3. create a temporary file servers_temp.dat
4. Read servers.dat line by line and copy each line to servers_temp.dat till you reach the line to be deleted
(keep incrementing a counter for each line)
5. When you reach the line to be deleted, skip that line without writing into servers_temp.dat
6. Copy all lines from servers.dat to servers_temp.dat
7. delete servers.dat
8. rename servers_temp.dat to servers.dat

Hope I am clear.
Shall try to post thecode , but need some time

0
 
deathman5Author Commented:
Well I thought of that, but it doesn’t work in my case
This is the .dat file

“server1”,”port1”
“server2”,”port2”
“server3”,”port3”

In the list box I have:

Server1
Server2
Server3

The port is not shown, so I can’t copy from list box and paste in another file, this would only copy the server; I need to copy the port too…
0
 
sgayatriCommented:
Well,
I never told you to copy from list box.
All you have to do is , note the "LISTINDEX"
whenever a delete is done

Add a command button for delete,

In command1_click
 
msgbox list1.listindex+1
list_index_number = list1.listindex+1

will give you the number of the line clicked.

use list_index_number  to delete that number line in your file.



0
 
David LeeCommented:
Why store the server names twice, once in the Ser array and again in the ListBox?  Instead, I'd create a second ListBox for the ports and set its Visible property to false.  Eliminate the Ser and Por arrays and read the data directly into the two ListBoxes.  When the user deletes one of the servers, you delete the values from both ListBox controls at the same time.  Something like:

    Private Sub DeleteServer()
            PorList.RemoveItem (SerList.ListIndex)
            SerList.RemoveItem (SerList.ListIndex)
    End Sub

We remove the entry from the Ports ListBox first because when we remove the entry from the Servers ListBox its ListIndex will change.

This will keep the two lists in sync.  Now when you exit the program all you need do is loop through the Servers ListBox and use its ListIndex value to get the corresponding Ports value and write them to the file.  Not only does this make your coding simpler, it also eliminates storing the same data twice, once in the array and again in the ListBox which will make your program smaller and faster.
0
 
VBtormentCommented:
Hai,

try this

on delete just do: Ser(index)=""
then save complete list, not just delete one from list...

'Load is good
Open "Servers.dat" For Input As #1
    Do Until EOF(1) = True
        Input #1, Ser(TotalWords), Por(TotalWords)
        TotalWords = TotalWords + 1
    Loop
Close

'for Saving try this
kill "Servers.dat"
Open "Servers.dat" For Append As #1
    for i%=1 to TotalWords
        if Ser(i%)<>"" then
            Write #1, Ser(i%), Por(i%)
        end if
    next i%
Close


hope this helps
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

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