Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    fprint - alternatives to printing at end of file

    Hi,

    would like to know if there is an alternative function or sidestep that allows you to print to a text file which is a list of indexed records other than appending a new record to the end of the file. i.e. place the new record in order:


    JOB: 001 ITEM 0 description
    JOB: 002 ITEM 0 description
    JOB: 003 ITEM 0 description

    would like to place the record
    JOB: 002 ITEM 1 description after
    JOB: 002 ITEM 0 description

    instead of at the end of the file

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,464
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    How about appending it, but the next time you read your file
    into an array, you sort it first ... so in summary, your file can be
    all mixed-up, but your array (that you work with) is always sorted fine.

    option two ...

    Do this each time a change is made.
    Read the file into array, sort it, write it back.

    option three ...

    use MySQL instead of flat-file text files.
    (a bit more of a learning curve here, but the best way to store data).

  • #3
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    There is no function to do this. You would need to write your own code.

    How this is typically done, is create a new temporary file with some arbitrary/random file name. Open your current file, read from it and write to the temporary file, parsing through the content until you find the position where the new line should be inserted. Write your new line to the temporary file. Read the remainder of your existing file and write it to the temporary file. When you are done, close the files and make sure there are no errors. If the temporary file was written successfully, delete your current file and rename the temporary file to replace your current file.

    If your file is not expected to grow to more than a few MB, you can probably read it into a variable/array all at once and insert the new line when you write it back out to a file. If your file is expected to continue to grow (you should switch and use a database) you will need to read and write manageable chunks of it at one time.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #4
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the feedback

  • #5
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by CFMaBiSmAd View Post
    There is no function to do this. You would need to write your own code.

    How this is typically done, is create a new temporary file with some arbitrary/random file name. Open your current file, read from it and write to the temporary file, parsing through the content until you find the position where the new line should be inserted. Write your new line to the temporary file. Read the remainder of your existing file and write it to the temporary file. When you are done, close the files and make sure there are no errors. If the temporary file was written successfully, delete your current file and rename the temporary file to replace your current file.

    If your file is not expected to grow to more than a few MB, you can probably read it into a variable/array all at once and insert the new line when you write it back out to a file. If your file is expected to continue to grow (you should switch and use a database) you will need to read and write manageable chunks of it at one time.
    Wow your are making that way more complicated than it is. I suggest taking mlseim's advice, there is no need for all those temp files.

  • #6
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    In real world application programming, where errors can occur and file sizes can grow, you never assume that your file will always be small enough to read into available memory all at once and you never overwrite a data file with an empty new file without first having a copy of your original file that you can fall back to if an error, a power failure, a memory glitch, a server lockup... should occur. Renaming the original file and writing to a new file and deleting the original file only after successfully checking that the new file is correctly written is common programming practice, especially if ALL of your valuable data only exists in this one data file.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #7
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I agree that using the temporary file is far more stable then any of the other solutions. A lot of programs work this way.

  • #8
    Regular Coder
    Join Date
    Oct 2005
    Location
    Right Here
    Posts
    654
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Yea I see that point, but thats a lot of data using txt files. (IF they are storing that much info). Wouldn't a database be better suited for holding that much information?

    I love different approaches and aspects

  • #9
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    3,020
    Thanks
    2
    Thanked 313 Times in 305 Posts
    Quote Originally Posted by iLLin View Post
    Wouldn't a database be better suited for holding that much information?
    Already suggested that -
    Quote Originally Posted by CFMaBiSmAd View Post
    If your file is expected to continue to grow (you should switch and use a database) you will need to read and write manageable chunks of it at one time.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #10
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Sometimes a database is not possible.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •