#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    51
    Rep Power
    15
    I am trying to write a portion of the script that will replace a line with a new one in a text file. I figured fputs would do the trick, but I tried opening the file with fopen with the mode r, r+, and w+ and they don't seem to do the trick. r will only read, r+ is supposed to read and write, but instead it just reads (nothing new was inserted when I tried it), and w+ erases the file then inserts the new information.

    The way the script works, it will search for the proper line using a while loop, once found, it will take the information and modify it slightly, then it's supposed to overwrite the new line over the old one, afterall, the pointer have not changed it's position since it found the correct line.

    I don't want to replace an entire file because the script will do more work, just one frigging line in the same position. Can anyone help me? Please...

    ------------------

    Yanik!
    Edmonton, AB

    [This message has been edited by yduvoid (edited August 03, 2000).]
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    51
    Rep Power
    15
    a little help here...
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    51
    Rep Power
    15
    Let me know if this can or cannot be done. pretty PLEASE!
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    France
    Posts
    16
    Rep Power
    0
    the modes are the following:

    'r' - Open for reading only; place the file pointer at the beginning of the file.

    'r+' - Open for reading and writing; place the file pointer at the beginning of the file.

    For r attributes the file must exists.

    'w' - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist,
    attempt to create it.

    'w+' - Open for reading and writing; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not
    exist, attempt to create it.

    'a' - Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it.

    'a+' - Open for reading and writing; place the file pointer at the end of the file. If the file does not exist, attempt to create it.


    In your case you should use a+
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    51
    Rep Power
    15
    well dammit, I don't want to place the information at the end of the list, there must be a way to place the info in the middle of the list... isn't there?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    London/UK
    Posts
    91
    Rep Power
    15
    1) try this and see if it helps
    http://uk.php.net/manual/function.fseek.php

    int fseek (int fp, int offset [, int whence])

    Sets the file position indicator for the file referenced by fp.The new position, measured in bytes from the beginning of the file, is obtained by adding offset to the position specified by whence, whose values are defined as follows:

    2) horrid, but workable, read in the file and (if it's to big to sit in memory) write it out to a temp file, if the line in question mathches whatever criteria you have, you can write out your new value to the temp file, then rm the old one and mv the new one to the old files name

    hth

    Bealers
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    51
    Rep Power
    15
    I took the second option, creating a new file and then copy it over the original by lines.

    The point was that I did not want the program to do that many work, but I can use more files to shorten the list, thus speeding up the proccess. A new file in each directory should work nicely, at least better than having one file for each page. Although, by far, a database would work best.

    If you're wondering, the script is a counter that will keep track of the number of hits.

    The counter is very basic, it keeps track of every single hit, no matter what the user did with the site. If you have any suggestions about counters, I'd be happy to hear them.

    ------------------

    Yanik!
    Edmonton, AB

    [This message has been edited by yduvoid (edited August 09, 2000).]

Similar Threads

  1. retrieve a line from a file
    By aleens in forum C Programming
    Replies: 10
    Last Post: February 26th, 2004, 05:05 AM
  2. What is the maximum line length for .csv file?
    By bnow in forum PHP Development
    Replies: 0
    Last Post: February 17th, 2004, 07:40 AM
  3. searching for a specific line in a text file
    By mattcronin57 in forum Python Programming
    Replies: 5
    Last Post: February 11th, 2004, 07:35 AM
  4. Opening file, finding line, then inserting new line
    By mach5 in forum Perl Programming
    Replies: 2
    Last Post: February 4th, 2004, 11:00 PM
  5. How to read multiple line text file into a variable
    By gatewaycityca in forum Visual Basic Programming
    Replies: 4
    Last Post: January 29th, 2004, 12:26 PM

IMN logo majestic logo threadwatch logo seochat tools logo