Click to See Complete Forum and Search --> : reading and writing
Danny
01-23-2003, 11:25 PM
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:(
druss
01-23-2003, 11:46 PM
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:)
Danny
01-24-2003, 12:17 AM
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:)
druss
01-24-2003, 06:47 AM
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