append one line of file

  • Thread starter Thread starter Gaby Sandoval
  • Start date Start date
G

Gaby Sandoval

I have this code.
The user can read teh record from the text file just fine. They can
click the NEXT button and it reads the next record (which is just the
next line from the text file).
If the user makes changes to a record using the application they have
to option to click the APPEND button to overwrite there changes on the
file. The file has a line of text for each record....... Here is the
code for when they click the APPEND button:

If filename = String.Empty Then
MessageBox.Show("No file has been selected this session."
& ControlChars.NewLine & "Please click 'Read First Record' to load
file.", "File Absence", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
response = cdlOpenFile.ShowDialog
Do While response = Windows.Forms.DialogResult.Cancel
MessageBox.Show("No file was selected." &
ControlChars.NewLine & "Please select file to append record to.",
"File Absence", MessageBoxButtons.OK, MessageBoxIcon.Error)
response = cdlOpenFile.ShowDialog
Loop

appendfilename = cdlOpenFile.FileName

My.Computer.FileSystem.WriteAllText(appendfilename, Name&
", " & Address & ", " & City & ", " & State & ", " & Zip, True)
End If

This only adds a line to the end of the document. If set to false, it
erases all the other lines of the text file. How do I get it to only
replace the one line that they are reading.
 
I have this code.
The user can read teh record from the text file just fine. They can
click the NEXT button and it reads the next record (which is just the
next line from the text file).
If the user makes changes to a record using the application they have
to option to click the APPEND button to overwrite there changes on the
file. The file has a line of text for each record....... Here is the
code for when they click the APPEND button:

If filename = String.Empty Then
MessageBox.Show("No file has been selected this session."
& ControlChars.NewLine & "Please click 'Read First Record' to load
file.", "File Absence", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else
response = cdlOpenFile.ShowDialog
Do While response = Windows.Forms.DialogResult.Cancel
MessageBox.Show("No file was selected." &
ControlChars.NewLine & "Please select file to append record to.",
"File Absence", MessageBoxButtons.OK, MessageBoxIcon.Error)
response = cdlOpenFile.ShowDialog
Loop

appendfilename = cdlOpenFile.FileName

My.Computer.FileSystem.WriteAllText(appendfilename, Name&
", " & Address & ", " & City & ", " & State & ", " & Zip, True)
End If

This only adds a line to the end of the document. If set to false, it
erases all the other lines of the text file. How do I get it to only
replace the one line that they are reading.

As the old saying goes, "You can't get there from here." In this case,
you simply cannot just insert a record in a text file. You will have
to open a temp file, write all records from the input file including
the record you want to insert after. Then write the new record, and
then write out all the rest of the records in the input file. Close
both files, kill the original file, and rename the new temp file to
the old filename.
 
Gabriel, maybe you can load all your text file (depend of the size) in a
array, each cell content a line of your file.

You can work all in the memory, and when the user update the data, then you
update your file; Other solution is load all the file like string, add the
new data, and save the modified string.

My best regards, you speak spanish?.

Freddy Coal
 
Back
Top