×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

edit a file....

edit a file....

edit a file....

(OP)
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....  

How do I do this....

RE: edit a file....

In Perl, you would use the open(FILEHANDLE) "function".

Here's an example script that opens a file, and prints a user name and password to it.


#!perl -w


print "Enter an user name:\n";
$user = <STDIN>; # get keyboard input and name it $user
chomp($user) # remove the trailing new line
print "Enter a password:\n";
$pass = <STDIN>;
chomp($pass);

# ok, here is where the "magic" happens

open(FILE, "<<file.txt") ¦¦ die("can't find file");
print FILE "$user : $pass\n";
close(FILE);

print "Thank you, $user, for entering your password, $pass, into the file\n";

exit;




Explanation: The code: open(FILE, "<<file.txt") ¦¦ die("can't find file");
print FILE "$user : $pass\n";
close(FILE);


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.

The next line, print FILE "$user : $pass\n"; prints the prints the username entered and the password entered, into the file, seperated by a colon.

And, finally, the last line, close(FILE); closes the file so that no more changes can be made.

There is a lot more that you can do with files, I hope this helps.


Vic.

RE: edit a file....

(OP)
yeah it does thanks.  I'll ask more questions if I need to...

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close