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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Modifying 1 line of file. 1

Status
Not open for further replies.

yolond

Technical User
Jan 5, 2001
9
If I know the line # in my file of the line I want to modify, how do I modify it and put it back?

This is what I have so far, but don't know what to do next.

Code:
open(LIST, $users) || &ErrorMsg("Could not open users file.");
@data=<LIST>; 
close(LIST);
print &quot;Content-type: text/html\n\n&quot;;

chop(@data[$id]);
($user,$company,$pass)=split(/\:/,@data[$id]);

print qq~
<table>
	<tr><td align=right><$font color=009680><b>Username: </b></font></td><td><input type=&quot;text&quot; name=&quot;username&quot; value=&quot;$user&quot; size=20></td></tr>
	<tr><td align=right><$font color=009680><b>Provider: </b></font></td><td><input type=&quot;text&quot; name=&quot;provider&quot; value=&quot;$company&quot; size=20></td></tr>
	<tr><td align=right><$font color=009680><b>Password: </b></font></td><td><input type=&quot;password&quot; name=&quot;password&quot; value=&quot;$pass&quot; size=20></td></tr>
	<tr><td colspan=2 align=center><br><input type=&quot;submit&quot; name=&quot;submit&quot; value=&quot;Modify User Info&quot;></td></tr>
~;
}
 
This isn't as easy as it sounds actually. There's no easy way to change just the one line of a variable record length text file, which most text files are.

What you have to do is read the file into memory (an array probably), close the file, make your changes, open the file for writing and write the whole thing back.

As long as the file isn't too big that approach works OK.

If the file *is* too big you have to:

Read the file, line by line, and write it out to a second file - complete with your changes, as you go. When you're done, delete the first file and rename the file you just created to have the same name as the first file. Mike
michael.j.lacey@ntlworld.com
Email welcome if you're in a hurry or something -- but post in tek-tips as well please, and I will post my reply here as well.
 
How do you hold the information (the lines) before and after the line you are modifying to stick the line back in at the right place?
 
you have to write to a temp file and then overwrite the original. something like that ...

open(DATABASE1,&quot;</redirections.txt&quot;); ## To Read
open(DATABASE2,&quot;>/redirections-temp.txt&quot;); ## To Write
@REDIRECTIONS = <DATABASE1>;

for ($i=0;$i<$#REDIRECTIONS+1;$i++) {
chomp($REDIRECTIONS[$i]);
if ( this is your line) {
change the line
print DATABASE2 &quot;$REDIRECTIONS[$i]&quot;;
}
else { print DATABASE2 &quot;$REDIRECTIONS[$i]&quot;; }

}

close(DATABASE1);
close(DATABASE2);
system(&quot;/bin/cp /redirections-temp.txt /redirections.txt&quot;);
 
Thank you Mike and Stefano! With Mike's explanation and Stefano's code, I was able to get the following code working perfectly. Since this file will never be huge, I went with your first suggestions Mike, simply overwriting the file. Wow, I spent 2 frustrating days over this routine. It turned out to be so simple.

Code:
open(INPUT, &quot;<$users&quot;);
@data = <INPUT>;
close(INPUT);

open (OUTPUT, &quot;>$users&quot;);
flock(OUTPUT, 2);

for ($i=0;$i<$#data+1;$i++)    {
    chomp($data[$i]);
    if ( $id eq $i )    { 
		$data[$i] = &quot;new data&quot;;
        print OUTPUT &quot;$data[$i]\n&quot;;
    } 
    else  { print OUTPUT &quot;$data[$i]\n&quot;; }
    
}

flock(OUTPUT, 8);
close(OUTPUT);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top