Open an existing DB_Ffile file failed

Discussion in 'Perl Misc' started by panxd01@gmail.com, Apr 14, 2007.

  1. Guest

    Hi, I tried to open an existing DB_File type of file to read the
    records. However, it kept getting error saying "cannot open file: File
    exists". Did some research on web but couldn't find any useful info.
    Could someone please explain why? Below is a sample of code which I
    got from the perl.com:

    use strict ;
    use DB_File ;
    use vars qw( %h $k $v ) ;

    tie %h, "DB_File", "fruit", O_RDWR|O_CREAT, 0640, $DB_HASH
    or die "Cannot open file 'fruit': $!\n";

    # Add a few key/value pairs to the file
    $h{"apple"} = "red" ;
    $h{"orange"} = "orange" ;
    $h{"banana"} = "yellow" ;
    $h{"tomato"} = "red" ;

    # Check for existence of a key
    print "Banana Exists\n\n" if $h{"banana"} ;

    # Delete a key/value pair.
    delete $h{"apple"} ;

    # print the contents of the file
    while (($k, $v) = each %h)
    { print "$k -> $v\n" }

    untie %h ;

    When I second time run the code, I got into the error because 'fruit'
    was created before. My perl version is 5.8.8 if this makes difference.
    Thanks a lot.
     
    , Apr 14, 2007
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Regina

    open an existing file

    Regina, Nov 17, 2005, in forum: Java
    Replies:
    6
    Views:
    7,607
    Regina
    Nov 18, 2005
  2. nrm
    Replies:
    3
    Views:
    534
  3. Tony Johansson
    Replies:
    3
    Views:
    16,240
    Patrice
    Jan 2, 2010
  4. x1
    Replies:
    0
    Views:
    372
  5. Replies:
    0
    Views:
    190
Loading...

Share This Page