Contact US

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!

*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

Modifying 1 line of file.

Modifying 1 line of file.

Modifying 1 line of file.

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.

open(LIST, $users) || &ErrorMsg("Could not open users file.");
print "Content-type: text/html\n\n";


print qq~
    <tr><td align=right><$font color=009680><b>Username: </b></font></td><td><input type="text" name="username" value="$user" size=20></td></tr>
    <tr><td align=right><$font color=009680><b>Provider: </b></font></td><td><input type="text" name="provider" value="$company" size=20></td></tr>
    <tr><td align=right><$font color=009680><b>Password: </b></font></td><td><input type="password" name="password" value="$pass" size=20></td></tr>
    <tr><td colspan=2 align=center><br><input type="submit" name="submit" value="Modify User Info"></td></tr>

RE: Modifying 1 line of file.

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.

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.

RE: Modifying 1 line of file.

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?

RE: Modifying 1 line of file.

you have to write to a temp file and then overwrite the original. something like that ...

open(DATABASE1,"</redirections.txt");      ## To Read
open(DATABASE2,">/redirections-temp.txt"); ## To Write

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

system("/bin/cp /redirections-temp.txt /redirections.txt");

RE: Modifying 1 line of file.

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.

open(INPUT, "<$users");
@data = <INPUT>;

open (OUTPUT, ">$users");
flock(OUTPUT, 2);

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

flock(OUTPUT, 8);

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