Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

edit a file....

Status
Not open for further replies.

leo2180

IS-IT--Management
Jun 7, 2000
52
0
0
US
Hey I want to edit a file.&nbsp;&nbsp;I want to go in the file take stuff out and then save the file, and or create another file with the new info....&nbsp;&nbsp;<br><br>How do I do this....
 
In Perl, you would use the open(FILEHANDLE) &quot;function&quot;.<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 &quot;Enter an user name:\n&quot;;<br>$user = &lt;STDIN&gt;; # get keyboard input and name it $user<br>chomp($user) # remove the trailing new line<br>print &quot;Enter a password:\n&quot;;<br>$pass = &lt;STDIN&gt;;<br>chomp($pass);<br><br># ok, here is where the &quot;magic&quot; happens<br><br>open(FILE, &quot;&lt;&lt;file.txt&quot;) ¦¦ die(&quot;can't find file&quot;);<br>print FILE &quot;$user : $pass\n&quot;;<br>close(FILE);<br><br>print &quot;Thank you, $user, for entering your password, $pass, into the file\n&quot;;<br><br>exit;<br><br></font><br><br><br>Explanation: The code: <FONT FACE=monospace> open(FILE, &quot;&lt;&lt;file.txt&quot;) ¦¦ die(&quot;can't find file&quot;);<br>print FILE &quot;$user : $pass\n&quot;;<br>close(FILE); </font><br><br>opens the file named file.txt. If the file does not exist, then it will display the line &quot;can't find file&quot;, in which case, you can create a blank text file.<br><br>The next line, <FONT FACE=monospace>print FILE &quot;$user : $pass\n&quot;;</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>
 
yeah it does thanks.&nbsp;&nbsp;I'll ask more questions if I need to...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top