For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2005 > RE: print to a specific location in a file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author RE: print to a specific location in a file
Brian Volk

2005-08-02, 4:59 pm



-----Original Message-----
From: Brian Volk
Sent: Monday, August 01, 2005 11:03 AM
To: Beginners (E-mail)
Subject: print to a specific location in a file


Hi All~

Is it possible to open a file and print to a specific location in the file?
For example, I have a 1K file and 0 - 511 is where the small description is
stored and starting in position 512 is the path to a pdf file.... If I do
not have the path in position 512 the link will be broken on web site. Pls
let me know if this is possible and where I can read about it.

Thanks!

Brian Volk
----------------------------------------------------------------------------
----
I think I found what I was looking for... ( s / read )

#!/usr/local/bin/perl

use strict;
use warnings;

my $txt_file = "C:/brian/descriptions/product/small/70413.txt";
open (FILE, $txt_file) or die "can't open $txt_file: $!\n";

my $link;

s (FILE, 256, 0);
read (FILE, $link, 150);

print $link;
---------------------------------------------------
now I just hope I can open the file replace the data in that byte
position... :~)

Thanks,

Brian


John W. Krahn

2005-08-02, 4:59 pm

Brian Volk wrote:
>
> Is it possible to open a file and print to a specific location in the file?
> For example, I have a 1K file and 0 - 511 is where the small description is
> stored and starting in position 512 is the path to a pdf file.... If I do
> not have the path in position 512 the link will be broken on web site. Pls
> let me know if this is possible and where I can read about it.
>
> I think I found what I was looking for... ( s / read )
>
> #!/usr/local/bin/perl
>
> use strict;
> use warnings;
>
> my $txt_file = "C:/brian/descriptions/product/small/70413.txt";
> open (FILE, $txt_file) or die "can't open $txt_file: $!\n";
>
> my $link;
>
> s (FILE, 256, 0);
> read (FILE, $link, 150);
>
> print $link;
> ---------------------------------------------------
> now I just hope I can open the file replace the data in that byte
> position... :~)


1) You need to open the file for both read and write.

open FILE, '+<', $txt_file or die ...;

2) You probably need to remove/add trailing padding when
reading/writing the data.

perldoc -f pack
perldoc -f unpack
perldoc -f substr

3) You should verify all system calls including s() and read().

4) You should probably include the s macros from the Fcntl module.



John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com