Solved

Adding text to file...

Posted on 2000-03-15
7
210 Views
Last Modified: 2010-03-05
I know how to add text to an existing fine, except it adds it to the end. How do I add it to the beginning of the file??
0
Comment
Question by:cdc_sickle
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +1
7 Comments
 
LVL 8

Expert Comment

by:jhurst
ID: 2621899
There is no direct way to do it, sadly
0
 
LVL 8

Accepted Solution

by:
jhurst earned 50 total points
ID: 2621905
The problem is that the underlying operating system file system does not usually have an 'open and insert at start'.  Where as, as you know there is the >> to add to the end.

So, what you will need to do is read the old data and insert the new then re-write the whole thing out.

Something like:
open(FIL,"<theFile");
@=<FIL>; gets what was there
close(FIL);
open(FIL,">theFile");
print "your new data\n";
print @x;
close(FIL);

I realize that this is not what you want.  Sadly, it is all you can have.
0
 
LVL 84

Expert Comment

by:ozo
ID: 2622200
perldoc -q append
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:craig_capel
ID: 2623269
another way....

Crude, but hey it works!!!

Stick an invisble memo onto your form, so the user can not see whats happening.... and do this..

memo1.lines.loadfromfile('c:\autoexec.bat');
memo1.lines.insert(0,'rem Hello');
memo1.lines.insert(1,'rem craig');
memo1.lines.insert(2,'rem was');
memo1.lines.insert(3,'rem here');
memo1.lines.savetofile('c:\autoexec.bat');


Stick it somewhere.... And thats the easiest way of doing it!....

Craig C. Jhurst, un Comment it as an Answer!

0
 
LVL 2

Expert Comment

by:craig_capel
ID: 2623276
SOOOOOOOORRRRRRRRRYYYYYY, thought i was in the Delphi section still.....  i really am sorry
0
 

Author Comment

by:cdc_sickle
ID: 2623551
Jhurst, that is exactly how i panned on having to do it. Ill see if it works!
0
 

Author Comment

by:cdc_sickle
ID: 2623616
This worked, with a little touching up!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

710 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question