?
Solved

Update Text file

Posted on 2003-03-23
3
Medium Priority
?
235 Views
Last Modified: 2013-12-25
I have a text file containing username and passwords in the format like below

user1:password
user2:password
user3:password

How can i change the passowrd in the text file

user1:password
user2:changedpassword
user3:password

plz give the example with code bcz i know little about CGI.

Thanx
WASIF
0
Comment
Question by:Muhammad Wasif
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 1

Accepted Solution

by:
nbthanh earned 80 total points
ID: 8193780
Hi wasifg,
This should be nothing to do with CGI since you can do this with a little Perl :-)

Here is an quick-n-easy way to do this:

- First, read the whole file into a hash:
my $buf = {};
open(FILE, "yourfile.txt") or die "cannot open yourfile.txt!";
while (my $line = <FILE>) {
  chomp $line; #chop out the new line character
  my ($user, $password) = split(/:/, $line);
  $buf->{$user} = $password;
}
close(FILE);

- Second, change the password
Assuming that $theuser is the user who wanted to change password and $newpassword is the new password.
if (exists $buf->{$theuser}) {
  $buf->{$theuser} = $newpassword;

  #write back to file
} else {
  #you can stop here
}

- Last, write back to file
open(FILE, ">yourfile.txt") or die "cannot open yourfile.txt!";
foreach my $k (keys %$buf) {
  print FILE, "$k:", $buf->{$k}, "\n";
}
close(FILE);

Please change yourfile.txt into you actual file's name

Hope this help.
0
 
LVL 20

Author Comment

by:Muhammad Wasif
ID: 8194841
Thanx friend

WASIF
0
 
LVL 1

Expert Comment

by:nbthanh
ID: 8195968
You're just welcomed :-)

If you're using text file for storing username/password, I recommend that you shouldnot use that for busy sites because multi simultaneous reads and writes might slow down your site alot, or even worse: corrupt your password file!
In this case you should use a database system :-)
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to provide a dynamic RTF document on your website generated with data from your database. For this tutorial you will need Microsoft Word or WordPad, WhizBase and Microsoft Access. In this tutorial I will show …
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question