Hey I want to edit a file. I want to go in the file take stuff out and then save the file, and or create another file with the new info.... <br><br>How do I do this....
In Perl, you would use the open(FILEHANDLE) "function".<br><br>Here's an example script that opens a file, and prints a user name and password to it.<br><br><FONT FACE=monospace><br>#!perl -w<br><br><br>print "Enter an user name:\n";<br>$user = <STDIN>; # get keyboard input and name it $user<br>chomp($user) # remove the trailing new line<br>print "Enter a password:\n";<br>$pass = <STDIN>;<br>chomp($pass);<br><br># ok, here is where the "magic" happens<br><br>open(FILE, "<<file.txt" ¦¦ die("can't find file"<br>print FILE "$user : $pass\n";<br>close(FILE);<br><br>print "Thank you, $user, for entering your password, $pass, into the file\n";<br><br>exit;<br><br></font><br><br><br>Explanation: The code: <FONT FACE=monospace> open(FILE, "<<file.txt" ¦¦ die("can't find file"<br>print FILE "$user : $pass\n";<br>close(FILE); </font><br><br>opens the file named file.txt. If the file does not exist, then it will display the line "can't find file", in which case, you can create a blank text file.<br><br>The next line, <FONT FACE=monospace>print FILE "$user : $pass\n";</font> prints the prints the username entered and the password entered, into the file, seperated by a colon.<br><br>And, finally, the last line, <FONT FACE=monospace>close(FILE);</font> closes the file so that no more changes can be made.<br><br>There is a lot more that you can do with files, I hope this helps.<br><br><br>Vic.<br>
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.