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!

open file append it then save it in the same directory

Status
Not open for further replies.

softworkz

Programmer
May 19, 2001
1
US
I have a perl program where the user enters in the file they want to "pad" variable length files to fixed length files based on the longest line,

EX: INPUT "variable"
I wish to pad this
file
into fixed
length.

EX: OUTPUT "fixed"
I wish to pad this
fileXXXXXXXXXXXXXX
into fixedXXXXXXXX
length.XXXXXXXXXXX

I have the code completed to do all of this but I need a way to take the file they entered and call it that filenamepad.ext? Here's what I have.

my $padchar = 0;
#this is given to the user from another program
print "enter the number of characters to pad to";
chomp ($padchar = <STDIN>);
print &quot;enter file to pad&quot;; #say padthis.txt
chomp ($file = <STDIN>);
open (FILE, &quot;<$file&quot;) or die &quot;can't open $!\n&quot;;
while (<FILE>){
$linelength = length ($_);
$bufferlength = $padchar - $linelength;
print $_;
for ($i=0; $i<$bufferlength; $i++){
print &quot;X&quot;;
}
close FILE;

it works sending to the screen but I need it to go to padthisdpad.txt leaving the original untouched. also I cant hard code the file name because each file that needs to be padded may have different extensions based on what ever they enter. Is there a way I can take the file they entered and rename it without hardcoding the filename? I hope I explained it well enough.
 
how are you calling the script? if it's like this:[tt]
>perl script.pl < filenamepad.txt
[/tt]
then you won't be able to read the file's name.
there are some things you need to know before this will work. the global variable @ARGV is where all command line arguments are stored. instead of redirecting the input of the script, pass in the name of the file to the script, from there open it and read it, then open up a new file with the desired name and write the new file contents to it. to call the script then, you will do this:[tt]
>perl script.pl filenamepad.txt
[/tt]
then, within the script you won't read or write from or to STDIN or STDOUT, you'll open the file stored in $ARGV[0] and read from that one instead of STDIN.[tt]
open(IN_FILE, &quot;<&quot; . $ARGV[0]) or die &quot;file failed to open:$!&quot;;[/tt]

then replace all the occurances of STDIN with IN_FILE. to open the file to write to, you'll need to change the filename slightly, using a regex like this:[tt]
my $out_file = $ARGV[0];
$out_file =~ s/^(.*)\./$1\.new/;
open(OUT_FILE, &quot;>&quot; . $out_file) or die &quot;ahhh! $!&quot;;
[/tt]
and replace your STDOUT's with OUT_FILE. i think this should all work, but i didn't really study your code carefully. good luck. &quot;If you think you're too small to make a difference, try spending a night in a closed tent with a mosquito.&quot;
 
Well, here's something else. You've already stored the filename from <STDIN>, so you can parse change the extension from that variable, use it to open a seperate filehandle a write your results. Here's how I'd implement this:

Code:
#!/usr/bin/perl

print &quot;enter the characters to pad with: &quot;;
chomp ($padchar = <STDIN>);
print &quot;enter file to pad: &quot;;
chomp ($file = <STDIN>);
open (FH, &quot;<$file&quot;) or die &quot;can't open $!\n&quot;;
@opened = <FH>;
close(FH);
@sort = sort map { sprintf &quot;%04s&quot;, length } @opened;
$file .= &quot;.ext&quot;;
open(OF,&quot;>$file&quot;);
foreach(@opened) {
    chomp $_;
    print OF $_ . $padchar x ( $sort[$#sort] - length ),&quot;\n&quot;;
}
close(OF);

This program reads in your padding character and the filename your padding. Opens up the file, stores each line in a array, figures out which line is longest. Then it opens a new filehandle with the same filename plus a .ext extension for writing and prints each line in the array to the file, padded with whatever characters the user initially entered. I haven't gotten a chance to test this out, but I'm pretty sure it works.

Hope this helps,

brendanc@icehouse.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top