www.webdeveloper.com
Results 1 to 4 of 4

Thread: reading and writing

  1. #1
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    20

    Unhappy reading and writing

    I need to know how to open a file, read the contents of the file, add a line of data to the end of the file then close the file.
    The file in questions has three varibles: id, name, email.

    Please help,

    DAnny

  2. #2
    Join Date
    Jan 2003
    Posts
    72
    This opens the document and stores the data in a variable:

    open(data,"yourfile.txt");
    @data = <data>;
    close(data);

    you then call the each line of the document through the variable like this:
    $data[linenumber] or @data for everything in the document.


    to add a line to the bottom of the text do the following:

    open(data,">>yourfile.txt");
    print data "info here";
    close(data);

    where >> adds a new line and > clears the document and then adds the line

    hope it helps

  3. #3
    Join Date
    Jan 2003
    Location
    Texas
    Posts
    20

    Talking read and write

    Thanks, it worked! I had forgotten about the >> and > it was the problem.

    Now that I have the file created, I need to take each line of data and seperate it out.
    id =2
    name = dan
    email = dan@test.net
    What is the best way to do it. I figured it had to do with split but I can't figure it out.

    Thanks

    Danny

  4. #4
    Join Date
    Jan 2003
    Posts
    72
    ok well then say that you store your info like this:
    $id & $name & $email

    then yes you will have to use split:

    try this one -

    @details = split(/&/, $data[number]);

    this will put the id in $details[0] and so on.

    hope it helps
    Goran

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles