Results 1 to 21 of 21

Thread: Deleting a record in a .txt file

  1. #1

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Unhappy Deleting a record in a .txt file

    Hi, I have a database system which works by entering a customers first name and second name, a1, a2, a3, postcode, etc..... each customers information is stored in separate *.txt files generated using the first and second name of the customer. deleting the customer files them self are not the problem as I use a simple KILL statement (if this is the wrong way to go about this please let me know)

    The the .txt file I am having trouble with is the one I am using to hold all the full names. these are writin out each time a customer is added and are placed on a new line every time and I should point out that they are being writin (as APPEND) as one STRING each time, which is takin from both the first and second name (text boxes)

    here is the code for the txt (that writes out the customer names):

    Code:
    Dim CustomersNames(0 To 999999) As String
    Code:
    Dim i As Integer
        i = 0
            Open "CUSTOMERS_NAMES.TXT" For Input As 1#
                Do While Not EOF(1)
                    Input #1, CustomersNames(i)
                    cmbFindCus.AddItem CustomersNames(i)
                Loop
                    cmbFindCus.Text = CustomersNames(0)
            Close #1
    End Sub
    here is the same file but for input into the program:

    Code:
    Dim i As Integer
        i = 0
    
        Open "CUSTOMERS_NAMES.TXT" For Input As 1#
            Do While Not EOF(1)
                Input #1, CustomersNames(i)
                cmbFindCus.AddItem CustomersNames(i)
                i = i + 1
            Loop
                cmbFindCus.Text = CustomersNames(0)
    Close #1
    and finally this is how I am trying and failing to delete one of the entity's:

    Code:
    Dim i As Integer
    i = 0
    
    Kill (App.Path & "\" & cmbFindCus.Text & ".txt")
    
    Open "CUSTOMERS_NAMES.txt" For Output As 1#
        Do While Not EOF(1)
    
            If i = cmbFindCus.ListIndex Then
               Write #1, (cmbFindCus.Text); (cmbFindCus.ListIndex)
               i = i + 1
            End If
    
    Write #1, CustomersNames(i)
               i = i + 1
        Loop
    
    cmbFindCus.Text = CustomersNames(0)
    
    Close 1#
    I should also add that when I test using this code it deletes everything that is in the file.

    Thanks in advanced, Seditives

  2. #2
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    In a nutshell, there is no quick way to delete a "record"
    in a .TXT file.

    But, here is a concept for a brute force method.

    Let's say you have 100 "records" (ie, 100 lines) in the file,
    and you want to delete line #76.

    1. Copy lines 1 to 75 of MyFile.txt to a new file, say Temp.txt.
    2. Skip line 76
    3. Copy lines 77 to 100 of MyFile.txt to Temp.txt.
    4. Then, "clear" out MyFile.txt, and copy all lines from Temp.txt to MyFile.txt

    Does that give you something to work with?

    Spoo

  3. #3

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    TY spoo, that does seem long winded but it seems pretty easy =] so in more coding terms it would be:

    1) open for input
    2) copy lines 0 - combo.listindex -1 into array
    3) copy lines (combo.listindex) + 1...... till EOF
    4) clear original
    5) write out to file agian

    first of all would this 5 step thing work and I also have two other questions:

    1) what sort of coding would I need to copy entities to a temp array?? a quick code snip-it would be awsome

    2) how do I clear a .txt I did this by accident in my attempt of deleting one entity XD but is there a clean way??

  4. #4
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    As for writing to temp array, maybe something like this ...

    Code:
    ' 0. set default values
    nn = combo.ListCount
    aa = 0
    fname = "CUSTOMERS_NAMES.TXT"
    ' 1. create temp array
    Dim aaTemp()
    ReDim aaTemp(nn)
    ' 2. open file from which to read
    Open fname For Input as #1
    ' 3. proceed 
    For ii = 0 to nn - 1
        Line Input #1, txt      
        If ii <> combo.ListIndex Then
            aa = aa + 1
            aaTemp(aa) = txt
        End If
    Next ii
    Close #1
    Conceptually, it goes through fname in one pass, reading
    line by line, and skips over the line that = combo.ListIndex.

    Note: I did not test this, so some tweaking may be required.

    I am using basically the same approach you did in your
    own code snippets, but with slight modifications to match
    with what I am more familiar with. Either way should be ok.

    Spoo

  5. #5

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    what is the ii variable doing? sorry but I am not familiar to this coding at all

  6. #6
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    The variable ii is acting as a counter.
    In this case, each time the loop interates, ii is increased by one.
    Hope that answers your question

    As an aside, using such a construct, loops can "jump", and even go backwards.

    For ii = 1 to 9 Step 2 -- ii would be 1, 3, 5, 7, 9
    For ii = 9 to 1 Step -2 -- ii would be 9, 7, 5, 3, 1

    Spoo

  7. #7

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    I see, TY for the advice spoo and I am I correct to think that do/loops and next statements basically just do the same thing in a structure like this?

  8. #8

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    Right I don't understand what I am doing wrong but it just clears out the whole txt file?

    Code:
    Dim WhatLine As Integer, DeletedCus As Integer
    
    DeletedCus = cmbFindCus.ListIndex
    WhatLine = 0
    
    Dim aaTemp()
    ReDim aaTemp(cmbFindCus.ListCount)
    
    Open "CUSTOMERS_NAMES.txt" For Input As #1
    Do While Not EOF(1)
        
        If WhatLine = DeletedCus Then
        WhatLine = WhatLine + 1
        End If
        
        Line Input #1, CustomersNames(WhatLine)
    WhatLine = WhatLine + 1
    Loop
    Close #1
    
    WhatLine = 0
    
    Open "CUSTOMERS_NAMES.txt" For Output As 1#
        Do While Not EOF(1)
        Write #1, aaTemp(WhatLine)
        WhatLine = WhatLine + 1
        Loop
    Close 1#
    Last edited by seditives; Jun 20th, 2011 at 02:35 PM.

  9. #9
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    I see 4 possible glitches:

    1. You never populated aaTemp() ..
    2. I think you want a separate counter for the array -- I used aa
    3. For your output, you used 1# instead of #1
    4. For your output, I think you want to use a loop based on the temp array, not on the file itself

    So, something like this, perhaps

    Code:
    Dim WhatLine As Integer, DeletedCus As Integer
    Dim aa As Integer             ' array counter
    aa = 0  
    DeletedCus = cmbFindCus.ListIndex
    WhatLine = 0
    Dim aaTemp()
    ReDim aaTemp(cmbFindCus.ListCount)
    Open "CUSTOMERS_NAMES.txt" For Input As #1
    ' 1. pop array
    Do While Not EOF(1)   
        Line Input #1, txt
        If WhatLine = DeletedCus Then
            b = nada              ' do nothing
        Else
            aaTemp(aa) = txt      ' populate array with text
            aa = aa + 1           ' increment array counter
        End If   
        WhatLine = WhatLine + 1
    Loop
    Close #1
    ' 2. re-pop file
    WhatLine = 0
    Open "CUSTOMERS_NAMES.txt" For Output As #1
    For ii = 0 to aa - 1
        Write #1, aaTemp(ii)
    Next ii
    Close #1
    As a possible glitch #5, I think you need to specify the full file path,
    such as "c:\myDir\mySubDir\CUSTOMERS_NAMES.txt"

    Hope that helps.

    Spoo
    Last edited by Spoo; Jun 20th, 2011 at 04:11 PM.

  10. #10
    PowerPoster Code Doc's Avatar
    Join Date
    Mar 2007
    Location
    Omaha, Nebraska
    Posts
    2,354

    Re: Deleting a record in a .txt file

    Please allow me to be the devil's advocate on this thread. Why delete the "record" in the first place?

    Instead, hold a 1-byte position on the record indicating whether to ignore or read it. If the record must be changed, append the new record to the file as the one that should be read in, and update the old record to ignore it.

    That way you avoid building a new file every time a record is updated. You can always periodically update the file with a new one that is packed a little tighter but not every time the file is updated.
    Doctor Ed

  11. #11
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Quote Originally Posted by Code Doc View Post
    Instead, hold a 1-byte position on the record indicating whether to ignore or read it.
    Hey, Doc

    All devils welcomed here.
    But, how do you do what you proposed?

    Spoo

  12. #12

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    Right spoo your code half worked, the problem is it seems to be rediming each line of the file or something along those lines. Here is what the file turns out like:

    (CUSTOMERS_NAMES.txt)

    """1 1"""
    """2 2"""
    """3 3"""
    """4 4"""
    """5 5"""
    """7 7"""
    """8 8"""
    """9 9"""
    """10 10"""

    quote: the two numbers on each line are representing first and second name

    as you can see it has got rid of the correct line but it seems to put 2 extra "" each time ? this is causing the combo box to display a couple of empty lines in between each customer???
    Last edited by seditives; Jun 21st, 2011 at 06:55 AM.

  13. #13
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    Well, we're at least making some progress.

    It is hard to tell exactly why you are getting the extra quotes
    without a little more information. Could you ..
    • post your modified code snippet
    • post a "before" version of CUSTOMERS_NAMES.txt --- or at least a demo version

    Spoo

  14. #14

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    OK, I am worn thin from this deleting of the line entity lol but its not your thought spoo as I have learned a lot from your posts but I think it's about time I just handed this in for an expert like your self to run through like you said because I am starting to pull my hair out lol (it sucks being a newbie to programing ) anyway I have attached the txt file with test information in. and also a little project that I have set up for the soul purpose of making this god damn thing work I have gave you all of the basic needs all that needs to be done is the coding for just one file.

    I am sure this is a easier option instead of me uploading the whole project or keep going back and forward on posts.

    thanks in advanced, Seditives
    Attached Files Attached Files
    Last edited by seditives; Jun 21st, 2011 at 07:45 AM.

  15. #15

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    sorry if you have downloaded the zip but i have made a mistake

    are be back with the right form layout shortly =/

    here we are ...
    Attached Files Attached Files
    Last edited by seditives; Jun 21st, 2011 at 07:56 AM.

  16. #16
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    Sorry, I'm knee deep in another project at present
    and don't really have time to go thru an entire zip file.

    But if you post snippets as you did earlier, I'll try to
    go thru them as before.

    If that doesn't work for you, then perhaps someone else
    can pick up the ball from here.

    Spoo

  17. #17

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    don't worry Spoo you have helped me a lot anyway. And I am not directing all the questions to you they are open to any one who can help =] are see what I can do for now and are post if I come up with a good solution =P

  18. #18

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    I am starting to get there now but still have some problems with the coding.

    this is how i am coding it at the moment

    Code:
    Dim CustomersNames(0 To 999999) As String
    
    Private Sub cmdDelete_Click()
    Dim i As Integer
    Dim DeletedCus As Integer
    
    i = 0
    DeletedCus = Combo1.ListIndex
    
    Open "C:\Users\Alex Winter\Desktop\Project1\CUSTOMERS_NAMES.txt" For Input As 1#
        Do While Not EOF(1)
    
          Input #1, CustomersNames(i)
               
               If i = DeletedCus Then
                  i = i + 1
               End If
               
        List1.AddItem CustomersNames(i)
        i = i + 1
        Loop
        
    Close 1#
    
    i = 0
    
    Open "C:\Users\Alex Winter\Desktop\Project1\CUSTOMERS_NAMES.txt" For Output As 1#
        For i = 0 To List1.ListCount
        Write #1, CustomersNames(i)
        Next i
    Close 1#
    
    End Sub
    I am having two issues:

    1) The "List1" shows all of the customers and leaves a blank row where the deleted customer used to be

    2) When a check the txt file after I have stoped running the program it still shows the customer I wanted to delete but with a blank entity under

    for example if I wanted to delete John Adam from 5 other customers it should show in the .txt file as:

    BEFORE------------------------AFTER

    "Other Customer"---------- "Other Customer"
    "John Adam"----------------"Other Customer"
    "Other Customer"----------"Other Customer"
    "Other Customer"----------"Other Customer"
    "Other Customer"

    but instead the AFTER looks like this?:

    "Other Customer"
    "John Adam"
    ""
    "Other Customer"
    "Other Customer"
    "Other Customer"

  19. #19
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    I think the logic error occurs here
    Code:
    Do While Not EOF(1)
        Input #1, CustomersNames(i)
        If i = DeletedCus Then
            i = i + 1
        End If
        List1.AddItem CustomersNames(i)
        i = i + 1
    Loop
    All that you have seem to have accomplished in
    your branch is to increment i by one. It does not
    seem to be skipping over anything.

    Below, I have changed the branch to only add
    to the list if i is NOT the DeletedCus
    Code:
    Do While Not EOF(1)
        Input #1, CustomersNames(i)
        If Not i = DeletedCus Then
            List1.AddItem CustomersNames(i)
        End If
        i = i + 1
    Loop
    Does that help any?

    Spoo

  20. #20

    Thread Starter
    Addicted Member seditives's Avatar
    Join Date
    Jan 2011
    Location
    South of England
    Posts
    151

    Re: Deleting a record in a .txt file

    I am sorry to say Spoo but it is still having a problem I have replaced the If statement with yours and this does seem to sort the space problem at least in the List Box but now doesn't change the txt file at all????

    I have thought the the code through and have came to the conclusion that it may be the fact that I am using the same dim'd array "CustomerNames" to INPUT and OUTPUT the Information, so the program may just be skiping over the List Box part and just writing the same thing back out agian, this is because I have read in from the file on form load (for the ComboBox) using the same dim'd array.

    In easy to understand terms (as I have been told I am not good at explaining code XD):::::::

    1) Program opens

    2) Reads in all info from the txt file into the one array I have (CustomersNames)

    3) Click on a customer name from the drop down

    4) I press the delete button

    5) the code you suggested runs, but, only to the benefit of the ListBox

    6) the program now forgets about that because its has done as it has been asked like a good boy.... or girl (I so hope programs aren't girls no offense if you are a girl reading this )

    7) the program then goes on to do the next thing set by its master (the writing out to the same file) but as it doesn't know my intentions it just uses the original CustomersNames array that was kindly introduced to the program by me at the start

    Sorry for being extremely patronizing but I hope this made you understand this the way my funny little brain saw this problem
    Last edited by seditives; Jun 22nd, 2011 at 01:17 PM.
    A subtle thought that is in error may yet give rise to fruitful inquiry that can establish truths of great value. - Isaac Asimov

  21. #21
    PowerPoster Spoo's Avatar
    Join Date
    Nov 2008
    Location
    Right Coast
    Posts
    2,656

    Re: Deleting a record in a .txt file

    Sed

    Aha .. I was wondering what CustomersNames(i) was all about.
    Now it is clearer

    Yes, so in case you have not already done so, it appears that
    you also want to change your Write algo from ...

    Code:
        For i = 0 To List1.ListCount
            Write #1, CustomersNames(i)
        Next i
    ... to something like this

    Code:
        For i = 0 To List1.ListCount
            Write #1, List1.ListIndex(i)
        Next i
    I have not worked with ListBoxes, so I'm not sure
    it that is the right syntax or property, but it should
    be close (hopefully)

    Also note that while you are properly doing ...

    Input #1
    Write #1

    ... you still have a syntax error in your Open and Close statements

    you have: Open 1#
    should be: Open #1

    ... same for Close

    EDIT:
    Apparently these syntax errors are not fatal, as you
    are at least able to populate the ListBox using Open 1#.
    But, I think you should change the code to prevent
    unexpected results.

    Spoo
    Last edited by Spoo; Jun 22nd, 2011 at 03:46 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width