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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing at the top of file

Status
Not open for further replies.

PhoenixDown

Programmer
Jul 2, 2001
279
CA
I use a flatfile db like:

1|test|test|test
2|test|test|test
3|test|test|test
4|test|test|test
5|test|test|test

And how would I writing lines at the begining instead of at the bottom? Thank you. :) AIM: XCalvin1984
Email: calvin@puremadnezz.com
MSN: xcloud2000x@hotmail.com
Web Site: Yahoo Messenger: xcloud2000x

Contact me for any programming help and whatnot. I'll try my best to help! :)
 
You can open the file and use seek to put your insert point at the top of the file. Check out the docs on seek. I don't remember how you need to open the file for read/write and it is past my bed time (getting sleepy |-I ).

Another fairly common way to achieve the same effect is to simply read the file contents into an array, add your new lines to the array and then print the new array back to the same file.

open(IPF,&quot;<yourDBF&quot;) or die;
@lines = <IPF>;
close IPF;

# add your new lines to the front of the array

open(OPF,&quot;>yourDBF&quot;) or die;
print OPF &quot;@lines&quot;;
close OPF;



HTH


keep the rudder amid ship and beware the odd typo
 
Thank you goBoating. I will try that when I program the rest of it. :) AIM: XCalvin1984
Email: calvin@puremadnezz.com
MSN: xcloud2000x@hotmail.com
Web Site: Yahoo Messenger: xcloud2000x

Contact me for any programming help and whatnot. I'll try my best to help! :)
 
Opening a file, seeking to the top and writing to it will simply OVERWRITE the first part of the file. There is no such thing as &quot;insert&quot; when you're working with a flat file. It's either overwrite or append.

The other method is the one you want. Although unless you have an overriding reason for keeping the file in that order, why not just reorder it when you read it in? That might be more efficient.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
If you use goBoating's suggestion, you can use one of the following approaches, depending on if you know the additional lines you wish to add before or after you read in the file:

ADDITIONAL LINES KNOWN BEFORE:
Code:
@lines = @extra;
while( <FILE> ) {
   push @lines, $_;
}

ADDITIONAL LINES KNOWN AFTER:
Code:
while( <FILE> ) {
    push @lines, $_;
}
unshift @lines, @extra;

Remember either to chomp the lines when reading in, OR ensure @extra lines contain &quot;\n&quot;

Hope this helps, cheers NEIL s-)
 
Unless you plan to process the file before writing it out, there really isn't any need to make a single array out of it. Just write the array of new lines before you rewrite the array of old lines. If you're adding to the end of the file you don't even need to read it, just open it in append mode. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top