?
Solved

Update Text file

Posted on 2003-03-23
3
Medium Priority
?
239 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
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Making a simple AJAX shopping cart Couple years ago I made my first shopping cart, I used iframe and JavaScript, it was very good at that time, there were no sessions or AJAX, I used cookies on clients machine. Today we have more advanced techno…
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

621 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