Home > Software engineering >  VB directly modify text file specified lines, not created another file.
VB directly modify text file specified lines, not created another file.

Time:10-04

VB directly modify text file to specify line, don't create another file, Private Sub Command1_Click ()
Dim TXT As String, I As Long
"1. TXT" Open For Input As # 1
Open "2. TXT" For the Output As # 2
The Do While Not EOF (1)
The Line Input # 1, TXT
I=I + 1
If I=3 Then TXT=Text1. Text + Text2. Text
Print # 2, TXT
Loop
The Close
End Sub
At present I am 1 read the contents of the file, then specify to line 3, no matter what, directly modify the content for both the contents of a text box, generated file 2, but I am not generated file 2, directly modify the file 1, but always make a mistake, please support,

CodePudding user response:

All the content read into the program,
Modify the content of the need to change,
Then close the external file,
Then kill and department documents,
Finally to write an external file.

CodePudding user response:

So there is certainly a problem

Can read all the rows, save to an array of strings, each element to save a line, then change line 3 (if there is line 3), the contents of the array to a file again

CodePudding user response:

Open in binary file, read and write and close, if the length of the same or increase, no problem,

CodePudding user response:

So-called modify delete file a certain position, is actually read the open files, a then 'a position before the modification and deletion of content + a + modification and deletion of the contents of the content of the modify delete position after b' save to file, and close the file. A, deleting a file, the file b changed its name to the same as the before file a name, that's it,

CodePudding user response:

What to do?
  • Related