Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I add a line to the top of an existing file? 1

Status
Not open for further replies.

Sina

Technical User
Jan 2, 2001
309
0
0
CA
How can I append a string to the top of a existing file.

I know how to append to the bottem of the file but how about appending it to the top, so it will be the first line.

Thanks you

I like to add the string =" serial number 12345678"

to the top of the file that already has data in there.
 
HI.


*copy the existing file (A) on a temporary file (B) (just a file with another name)
*open it (A) again with the option that erases what the file contains
*write on your line : "serial number ...."
*close A and open it again with the append parameter set (in order to be on the bottom of the file)
*write on the temporary file (B)
*close all
*destroy the temporary file B

Hope this help.
Bye
 

Hi, A very quick way also is

$filename = "test.db";
{
local $^I = "";
local @ARGV = $filename;
while (<>) {
print &quot;Top Line&quot; if $. == 1; # print befor line 1
print;
}
}

On windows machines you're not allowed to leave $^I undefined but on unix you can. It's the name of the backup file so if you take the line out by default it will make a backup file called test.db.bak

Tony
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top