Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

File Access

by dmckee (Scribe)
on Jan 12, 2001 at 16:02 UTC ( [id://51353]=perlquestion: print w/replies, xml ) Need Help??

dmckee has asked for the wisdom of the Perl Monks concerning the following question:

I've been playing with basic file access, and have come up against the problem that
print NOTES "test"; . . . $fromfile=<NOTES>; if ($fromfile eq "test") {print "Yes, this works"};
doesn't.
Should I try to strip, or should I be writing this differently? It's only single word information, really: and it needs to be platform independant as its CGI running on Windows for testing which will move to a Unix-esque box.

So what's the best approach?

Replies are listed 'Best First'.
Re: File Access
by davorg (Chancellor) on Jan 12, 2001 at 16:08 UTC

    You don't show us the code where you open the file. Without that I'd guess that you've only opened the file for reading or something like that.

    If you're attempting to read or write a filehandle which isn't open in the right mode, then Perl will tell you exactly what the problem is if you ask it to. You should always start your Perl scripts with

    #!/your/path/to/perl -w use strict;

    and then Perl with give you all the help it can.

    --
    <http://www.dave.org.uk>

    "Perl makes the fun jobs fun
    and the boring jobs bearable" - me

Re: File Access
by mirod (Canon) on Jan 12, 2001 at 16:24 UTC

    Your question raises plenty of problems.

    What exactly do you want to do? As the previous answers showed if you want to write to a file, then later on to read from it, you just need to close it in between. If you want to keep it open here is how you can do it:

    #!/bin/perl -w use strict; open( NOTES, "+<note.txt") # +< allows you to do read/writ +e updates on the file or die "cannot open note.txt: $!"; print NOTES "stuff\n"; # print stuff in your file my $pos= tell NOTES; # mark the position before the +write you are interested in print NOTES "test\n"; # write your data (it should en +d up with \n if you want # <NOTES> to pick it up properl +y later) # when you are ready to read it seek NOTES, $pos, 0; # reset filehandle to the right + position my $fromfile=<NOTES>; # read chomp $fromfile; # rememebr that \n? You might w +ant to remove it now if ($fromfile eq "test") {print "Yes, this works"}

    Now why would you want to do this over storing the data in an array or something similar, which would be way faster than playing with files? I have no idea (size of the data you actually write?)

Re: File Access
by jeroenes (Priest) on Jan 12, 2001 at 16:08 UTC
    There are two issues here.
    • Make sure NOTES is closed and opened for reading 're you reach $fromfile
    • You probably forgot to chomp a newline.
    Try this:
    . . while (<NOTES>){ print "Found 'test'\n" if (/test/); }
    It is more robust regarding newlines, other characters or other text.

    Hope this helps,

    Jeroen
    I was dreaming of guitarnotes that would irritate an executive kind of guy (FZ)

Re: File Access
by ChOas (Curate) on Jan 12, 2001 at 16:10 UTC
    Wanna try this, and see what happens ? :
    #!/usr/bin/perl -w use strict; open NOTES,">tmp.fil" or die "Aaaargh!\n"; print NOTES "test"; close NOTES; open NOTES "<tmp.fil" or die "Aaaaaargh2!\n"; my $Inpt=<NOTES>; close NOTES; print "Yup, it's there!\n" if ($Inpt eq "test");

    Your file access won't get much more basic than that ;))

    See what happens...

    GreetZ!,
      ChOas

    print "profeth still\n" if /bird|devil/;
Re: File Access
by repson (Chaplain) on Jan 12, 2001 at 16:16 UTC
    Here's all the syntax you need if you don't want to close the file in between read and write, look up the funtions in your perl documentation if need be.
    #!perl -w use strict; my $filename = "notes.txt"; if (-e $filename) { open NOTES, '+< ' . $filename or die "Cant open $filename: $!\n"; } else { open NOTES, '+> ' . $filename or die "Cant open $filename: $!\n"; } print NOTES "foo\n"; seek NOTES, 0, 0; $data = <NOTES>; print $data;
    You may also want to use flock depending on your applications.
Re: File Access
by I0 (Priest) on Jan 12, 2001 at 19:40 UTC
    open NOTES,"+>filename" or die $!; print NOTES "test"; seek NOTES,0,0; $fromfile=<NOTES>;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://51353]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (6)
As of 2024-03-28 20:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found