...

View Full Version : fprint - alternatives to printing at end of file



nicholas76
03-27-2007, 08:31 AM
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

mlseim
03-27-2007, 03:24 PM
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).

CFMaBiSmAd
03-27-2007, 03:33 PM
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.

nicholas76
03-28-2007, 04:59 AM
Thanks for the feedback

iLLin
03-28-2007, 05:16 AM
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.

CFMaBiSmAd
03-28-2007, 05:41 PM
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.

aedrin
03-28-2007, 05:49 PM
I agree that using the temporary file is far more stable then any of the other solutions. A lot of programs work this way.

iLLin
03-28-2007, 06:23 PM
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 :)

CFMaBiSmAd
03-28-2007, 06:28 PM
Wouldn't a database be better suited for holding that much information? Already suggested that -

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.

aedrin
03-28-2007, 09:09 PM
Sometimes a database is not possible.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum