Please..I need this. i am beginer.

Thank you in Advance.

How i can insert a text string into a specific location in a text file(Already exists) using delphi.

(without opening a temp. file and do it in a ineffective way)

ams4380361Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

pedeCommented:
Hi
You need to make a copy to a new file.

1. Read until you reach the position to insert the text. While you do this, you write the content to a new file

2. Write the text to insert to the new file

3. Read through the rest of the original file, while writing the content to the new file

4. Delete the original file, and rename the new file to the name of the original file

Sorry, I havent got time to give samples, Im just commenting. If anyone has a better solution, Id like to hear it, because Im about to do something similar soon, with very large files, and this method kinda sucks, but seems the only way ;o)

Regards,
Pede
0
ams4380361Author Commented:
Edited text of question.
0
DrDelphiCommented:
Load the file into a Tstringlist:

var MyFile:Tstringlist;
begin
   Myfile:=Tstringlist.Create;
   Myfile.LoadFromFile('c:\somefile.txt');
   Myfile.Insert(location,'Dave was here');
   Myfile.SaveToFile('C:\somefile.txt');

end;
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

edeyCommented:
As far as avoiding loading the entire file into memory the only solutions I can think of work only if the *size* of the file doesn't change, ie. you can change chars in the files, but not add or delete them


GL
Mike
0
pedeCommented:
DrDelphi: Your solution is very nice, but in my case, where the files can be well over 1GB, its not very "practical". I guess ams...'s files are smaller, since he accepts it ;)

Regards,
Pede
0
edeyCommented:
pede, why are your data files so large? is it not possible to use a db driver with them? or to split them?


GL
Mike
0
pedeCommented:
Edey: Long story ;-)  (almost as long as the files). I really have no option other than working with them. Splitting would only make things more difficult.

/Pede
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.