Problem adding new line !

W

Wg

Hi,

I'm trying to add a new line to a file say "tmp_file1.txt" before
reading into that file. Here tmp_file1.txt is the destination file
into which I'm trying to copy.. Pasting code snippet. Is there
anything that I'm missing out here ..

sub create_tmp_file1()
{

# Pls note: the function recieve's the file handle of the
source file and passess on the file handle of the #destination file to
the sub routine that is called for inserting new line.. Attempt is to
give sufficient spacing #between reading from each source file..


my $inputFile = $_[0];
my $getLine;
open (TMP_FH, ">>tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";
print $inputFile."\n";

&addNewLine(\*TMP_FH);

while(<$inputFile>){

$getLine = $_;
print TMP_FH $getLine;
}

$getLine = "\x0C";
print TMP_FH $getLine;


# print TMP_FH "\x0c";

close (TMP_FH);

}

# Function that adds new line character..

sub addNewLine(){

my $fileName = $_[0];
my $count = 0;

# open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
file ...";

while($count le 30){
#print TEMP_FH "\n";
#print TEMP_FH " \n";
print $fileName " \n";
$count++;
}

# close(TEMP_FH);

}

Any pointer to this would be greatly appreciated..

-Wg
 
S

smallpond

Hi,

I'm trying to add a new line to a file say "tmp_file1.txt" before
reading into that file. Here tmp_file1.txt is the destination file
into which I'm trying to copy.. Pasting code snippet. Is there
anything that I'm missing out here ..

You forgot to say "doesn't work" as the complete
explanation of what problem you might be having.
 
B

Ben Morrow

Quoth Wg said:
I'm trying to add a new line to a file say "tmp_file1.txt" before
reading into that file. Here tmp_file1.txt is the destination file
into which I'm trying to copy.. Pasting code snippet. Is there
anything that I'm missing out here ..

What's going wrong? It's not clear exactly what you're trying to
achieve, and you haven't told us what the code below is doing wrong. It
appears to be appending the contents of $inputFile, followed by 31 lines
with two spaces, followed by a form-feed character (with no following
newline, so the file is no longer a text file) to tmp_file1.txt.

Some general comments on the code follow, but I doubt they'll solve your
problem.
sub create_tmp_file1()

You don't want those parens: they don't do what you thing they do. Perl
isn't shell.

sub create_tmp_file1 {
{

# Pls note: the function recieve's the file handle of the
source file and passess on the file handle of the #destination file to
the sub routine that is called for inserting new line.. Attempt is to
give sufficient spacing #between reading from each source file..


my $inputFile = $_[0];
my $getLine;

It's generally bad practice to declare variables earlier than you need
them.
open (TMP_FH, ">>tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";

You will find dealing with filehandles much easier if you use the
lexical filehandles introduced in perl 5.6. For a start, you won't need
to mess around with globrefs.

You should use three-arg open: while it doesn't matter in this case,
it's a good habit to get into.

open(my $TMP_FH, ">>", "tmp_file1.txt")
or die "...";
print $inputFile."\n";

&addNewLine(\*TMP_FH);

Don't call subs with & unless you know what it does. This may explain
why you got away with the () on your sub declarations...

With lexical filehandles (opened as above) you don't need to use globref
syntax. Just pass the filehandle as it is:

addNewLine($TMP_FH);
while(<$inputFile>){

$getLine = $_;

It's clearer to assign directly into the right variable. As a bonus, you
don't stomp on the global $_.

while (my $getLine = said:
print TMP_FH $getLine;
}

$getLine = "\x0C";
print TMP_FH $getLine;

....of course, then $getLine is no longer in scope here. I would just go
back to printing it directly.

print $TMP_FH "\x0C";
# print TMP_FH "\x0c";

close (TMP_FH);

Another benefit of lexical filehandles is that they close for you when
the variable goes out of scope. When you're writing to a file, though,
you should check for errors on close, as any error while writing
(because of a full disk, for instance) will show up as an error on close.
(Of course, if you want to know exactly *what* failed to write, as
opposed to simply dieing with an error, you'll need to check the return
value of print as well.)
}

# Function that adds new line character..

sub addNewLine(){

my $fileName = $_[0];

This is not a file name, but a filehandle. I presume you know this, but
leaving bad variable names in your code is a sure way to confuse
yourself when you come back to it later (not to mention people on Usenet
trying to read your code).
my $count = 0;

# open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
file ...";

while($count le 30){

It's simpler to write this as a for loop (foreach if you prefer):

for my $count (0..30) {

Ben
 
W

Wg

Quoth Wg <[email protected]>:


I'm trying to add a new line to a file say "tmp_file1.txt" before
reading into that file. Here tmp_file1.txt is the destination file
into which I'm trying to copy.. Pasting code snippet. Is there
anything that I'm missing out here ..

What's going wrong? It's not clear exactly what you're trying to
achieve, and you haven't told us what the code below is doing wrong. It
appears to be appending the contents of $inputFile, followed by 31 lines
with two spaces, followed by a form-feed character (with no following
newline, so the file is no longer a text file) to tmp_file1.txt.

Some general comments on the code follow, but I doubt they'll solve your
problem.
sub create_tmp_file1()

You don't want those parens: they don't do what you thing they do. Perl
isn't shell.

    sub create_tmp_file1 {
       # Pls note: the function recieve's the file handle of the
source file and passess on the file handle of the #destination file to
the sub routine that is called for inserting new line.. Attempt is to
give sufficient spacing   #between reading from each source file..
   my $inputFile = $_[0];
   my $getLine;

It's generally bad practice to declare variables earlier than you need
them.
   open (TMP_FH, ">>tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";

You will find dealing with filehandles much easier if you use the
lexical filehandles introduced in perl 5.6. For a start, you won't need
to mess around with globrefs.

You should use three-arg open: while it doesn't matter in this case,
it's a good habit to get into.

    open(my $TMP_FH, ">>", "tmp_file1.txt")
        or die "...";
   print $inputFile."\n";
   &addNewLine(\*TMP_FH);

Don't call subs with & unless you know what it does. This may explain
why you got away with the () on your sub declarations...

With lexical filehandles (opened as above) you don't need to use globref
syntax. Just pass the filehandle as it is:

    addNewLine($TMP_FH);
   while(<$inputFile>){
           $getLine = $_;

It's clearer to assign directly into the right variable. As a bonus, you
don't stomp on the global $_.

    while (my $getLine = said:
           print TMP_FH $getLine;
   }
   $getLine = "\x0C";
   print TMP_FH $getLine;

...of course, then $getLine is no longer in scope here. I would just go
back to printing it directly.

    print $TMP_FH "\x0C";
#  print TMP_FH "\x0c";
   close (TMP_FH);

Another benefit of lexical filehandles is that they close for you when
the variable goes out of scope. When you're writing to a file, though,
you should check for errors on close, as any error while writing
(because of a full disk, for instance) will show up as an error on close.
(Of course, if you want to know exactly *what* failed to write, as
opposed to simply dieing with an error, you'll need to check the return
value of print as well.)
# Function that adds new line character..
sub addNewLine(){
   my $fileName = $_[0];

This is not a file name, but a filehandle. I presume you know this, but
leaving bad variable names in your code is a sure way to confuse
yourself when you come back to it later (not to mention people on Usenet
trying to read your code).
   my $count = 0;
#  open TEMP_FH, ">>".$fileName or die "Failed opening $outputFile
file ...";
   while($count le 30){

It's simpler to write this as a for loop (foreach if you prefer):

    for my $count (0..30) {

Ben

Thanks Ben !

I was trying to insert new line to the destination file between
reading each source file. Follow'd Ben's guidance on this.. Pasting
below the code snippet that worked for me..

sub create_tmp_file1{

open (my $TMP_FH,">>","tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";

addNewLine($TMP_FH);

my $inputFile = $_[0];
my $getLine;

while(<$inputFile>){

$getLine = $_;
print $TMP_FH $getLine;
}

print $TMP_FH "\x0C";

close ($TMP_FH);

}

sub addNewLine{

my $fileHandle = $_[0];

# open ($fileHandle, ">>", "tmp_file1.txt") or die "Failed opening
file ...";

for my $count(0 .. 10){

print $fileHandle " \n";

}

# close($fileHandle);

}

Thanks Ben for your time and patience..

-Wg
 
J

John W. Krahn

Wg said:
I was trying to insert new line to the destination file between
reading each source file. Follow'd Ben's guidance on this.. Pasting
below the code snippet that worked for me..

sub create_tmp_file1{

open (my $TMP_FH,">>","tmp_file1.txt") or die "Unable to open file
tmp_file1 for writing ..\n";

addNewLine($TMP_FH);

my $inputFile = $_[0];
my $getLine;

while(<$inputFile>){

$getLine = $_;
print $TMP_FH $getLine;
}

print $TMP_FH "\x0C";

close ($TMP_FH);

}

sub addNewLine{

my $fileHandle = $_[0];

# open ($fileHandle, ">>", "tmp_file1.txt") or die "Failed opening
file ...";

You had already opened the filehandle in create_tmp_file1() above so you
don't have to open it again.

for my $count(0 .. 10){

print $fileHandle " \n";

}

Or more simply:

print $fileHandle " \n" x 11;

Or just replace the line:

addNewLine($TMP_FH);

in create_tmp_file1() above with the line:

print $TMP_FH " \n" x 11;

# close($fileHandle);

}


John
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Command Line Arguments 0
Problem Splitting Text String 2
purge line 13
Merge files 1
Printing a new line 8
nice parallel file reading 14
pass array reference 3
How to read from URL line-wise? 3

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top