Deleting text from a file

Mikey B
05-29-2003, 11:50 AM
Hi i have a list of family members in a text file like this

Me
Mum
Dad
Sis
Bro

And i want to read one at a time and delete it once its finished reading it. So it will read 'Me' first then add it to a text box in VB and then deletes 'Me' from the original text file and goes onto the next one. However the next family member must then be at the begininning of the text file. So once it deletes 'Me' The text file is closed. Its then opened again and 'Mom' is at the top of the list.

This is the code ive got so far


dim counter as integer
Open "c:\famile.txt" For Input As #1
counter = 1
Do While Not EOF(1)
Input #1, name
Text2.Text = name
'code goes here to delete the line from the text file
close #1
loop


I admit that it wasnt explained very well so if you need any more info then just ask.

Mikey

loquin
05-29-2003, 12:00 PM
Open "c:\famile.txt" For Input As #1This line says it all. A text file can be opened for input or for output (append is a variation of output.) ANd that's it. No other choices.

If you must operate in the manner you discuss, you will need to create a temporary text file, and write all the lines into it that you DON'T want to delete. Then close the input file and the output file, Kill the old input file, and rename the new file with the original name.

Mikey B
05-29-2003, 12:05 PM
In that case can i tell the program which line of the text file to read from? Cause once i get the first line another form will open up and the one that red it will close... but when its opened again i want it to start from where it left off. Like on the main form i could output the value of counter to a text file. So if the counter was 1 (as it is at the beginning) this will get outputted to a new file and when the form loads again it will read the file, get the number of it and start reading from that line. Is that possible?

Sorry if this seems confusing and thanks for your help

loquin
05-29-2003, 12:14 PM
Then, don't worry about deleting the lines from the file. And don't bother with creating a new file just to hold the line number or info from that line. Just keep track of which line you currently want to use from the source file. Add a public variable to your main form to hold the position want to at the top of the code, before any subs, but after the Option Explicit directive. (btw - always use option explicit!)

Option Explicit

Public intLinePos as Integer

Private Sub...

In your main form, you will increment this value, so that it points to the line you wish to use.

Then, in your Sub-Form, you will loop through the file, reading it line by line, and increasing a local line counter. When your local line counter equals YourMainFormName.intLinePos, you're at the correct line.

Mikey B
05-30-2003, 04:56 AM
Is there anyway i can achieve this without looping through the file? What i want to do is have a timer that opens the file and reads the line that the counter is equal to. Once it gets the line it then closes the file and adds 1 to the counter. Next time the timer is called it checks the value of the counter and reads that line from the file.

Thanks

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum