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!

seek()

Status
Not open for further replies.

youradds

Programmer
Jun 27, 2001
817
0
0
GB
Ok, so im new to the seek command. Can someone please tell me though why the following command keep putting new data to the end of the file, but NOT at the beginninag as it should do?

The code is;

open(LIST, ">>southeast.list") || die &error("Unable to open file. Reason: $!");
flock(LIST, 2);
seek(LIST,0,0);
print LIST "$town\:\:\:$return\:\:\:$vehicle\:\:\:$mobile\:\:\:$hour\:\:\:$mins\n";
flock(LIST, 8);
close(LIST);

Thanks

Andy
 
After trying several options, I must admit that I coudn't
find the reason for it not working as it should.
The only thing that might help is - only in case the
file is relatively small - reading all the file contents
into an array (@lines = <FILE>), writing the required data
while file is open for rewrite (>) and then dumping the
array back to the original file while the file is opened
for append - a bit clumsy and not at all performance-oriented, but yet...

Regarding the original problem - maybe seek()
is not supposed to be used on text files ? Jean Spector
QA Engineer @ mSAFE Ltd.
 
I'm almost sure seek() is supposed to be used with read/write functions and not print() ... Jean Spector
QA Engineer @ mSAFE Ltd.
 
Ok. Thanks for trying. How do you reverse the contents of an array? That way it should be easier to reput them all in :)

Andy
 
Reversing an array is easy:

Code:
@reversed = reverse @array;

As for the seek problem, you are opening your file for appending (>>), so I'm not sure the seek is doing anything at all. Try opening the file for read/write (+>) and see if that helps. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
The joke is that it doesn't - my &quot;trying several options&quot; in the first post included this...
Jean Spector
QA Engineer @ mSAFE Ltd.
 
I think you're right about seek being used with read and write. That might be the problem. I do know that opening a file with >> is supposed to write to the end. I'm not sure seek is affecting that. As for seek not being used with text files, I don't think that's relevant - unix in particular doesn't care whether a file is text of binary. In fact, I think unix even ignores the binmode command. Even on a non-unix system though, text files can be used with seek/read/write, although then you might want to use the binmode command.

If you're trying to OVERWRITE records at the beginning of the file, seek/read/write should work (with +>), but unless the records are all fixed length that could cause problems. If you're trying to INSERT records at the beginning of the file, that can't be done. The only way is to either write the new records to a temporary file, then copy the old file to the temp file after them, then rename the temp file or copy it over the old one, or read the file into an array, insert the new records at the front of the array (or vice versa), and then rewrite the file (with >). PERL is very efficient at this sort of thing, so it shouldn't be too bad. The way perl handles memory you can read a pretty big file into memory without any real problems. You don't even have to use loops to read and write the file, just use the list forms:
Code:
open(FILEHANDLE,&quot;<yourfile&quot;);
@recs = <FILEHANDLE>;
close FILEHANDLE;
push(@recs, @newrecs);
open(FILEHANDLE,&quot;>yourfile&quot;);
print FILEHANDLE @recs;
close FILEHANDLE;
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