Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi rewriting a single line

Can we rewrite a single line of a textfile and then save it and close it?

For example, i need to rewrite the first line, and keep all the others. Is there a function to do this or do i have to copy the whole file after changing a single line?

My file contains more than a thausand lines, and i only need to change the first line.

Example of file:

test;test1;test2
other;other;other
other;other;other
x1000

and then

something;something;something
other;other;other
other;other;other
x1000

See what i mean? I just want to keep my file like it is but change the first line. I could copy the whole file and paste it after i changed the first line but i wonder if there a method already included in delphi to only change a particular line in a text file. Thanks!

like image 345
user28470 Avatar asked Oct 29 '25 16:10

user28470


1 Answers

This is not possible. Files are stored linearly and do not support insertion. If your line was a fixed length, then you could overwrite it. However, you wish to replace the line with new content that is longer. That cannot be done. You'd need to re-write the entire file.

A database may be more appropriate for your needs than a text file.

like image 150
David Heffernan Avatar answered Oct 31 '25 06:10

David Heffernan