Why open the file until enter has been pressed?

Try something like this

$file = "c:/test.txt";

while (1) {
    &stuff;
}

sub stuff {
    print "Enter some text for the file (q to quit): ";
    $input = <STDIN>;
    print "\n";

    exit if $input eq "q\n";

    open FILE, ">>$file" or die "Can't append to $file: $!";
    print FILE $input; # You don't need a \n as it's not been stripped from
the input. And you should be using chomp, not chop for that.
    close FILE;

    print "##File contents are now:\n";
    
    open FILE, $file or die "Can't read from $file: $!";
    print while (<FILE>);
    close FILE;
    print "##End of file\n\n";
}

HTH

John

-----Original Message-----
From: Bruce Ambraal [mailto:[EMAIL PROTECTED]] 
Sent: 04 April 2002 12:09
To: [EMAIL PROTECTED]
Subject: Opens / create file if necessary


Could you help I am not doing what I'm surpose to

With the code below I'm trying to do the following:
-Open a file, creating it  if necessary
-Print a name in file every time someone presses "enter"
-Print  a new line 
-Seek to the begining of file without closing file
-and print the file to STDOUT

----------------------------------------------------------------------------
------------------------------
#!/usr/bin/perl -w

open(INFILE,"+>>inline") || die "Could not open filename";
open(OUTFILE,"+>>outline") || die "Could not open filename";
         my $inline = <STDIN>; chop($inline);

                  print OUTFILE "$inline\n";
close(INFILE);
close(OUTFILE);



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]


--------------------------Confidentiality--------------------------.
This E-mail is confidential.  It should not be read, copied, disclosed or
used by any person other than the intended recipient.  Unauthorised use,
disclosure or copying by whatever medium is strictly prohibited and may be
unlawful.  If you have received this E-mail in error please contact the
sender immediately and delete the E-mail from your system.



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to