hi,
you may try writing in the file using .. seekOrigion
this may help to write latest comments at the beggining and then reading first the latest ones..
Main Topics
Browse All TopicsWriting a webapp. The user posts a comment, the comment is sent to a text file. When the page reloads the text file's contents are displayed to the user.
I want to have it so when the new comment is appended to the text file it is added at the top of the text file, not at the bottom. So that when the content of the text file is displayed to the user the newest additions will be on top.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
CuteBug: That requires reading the entire file into memory every time.
needo_jee: Your solution will overwrite the text at the top of the file (it won't insert).
I personally suggest that the best and most efficient way is to follow these steps:
1. Open a new temporary file for writing (OUT).
2. Write the new text to OUT.
3. Open old file for reading (IN).
4. Iterate through all lines in IN and write them to OUT.
5. Close OUT, close IN.
6. Rename OUT to IN.
Alternatively:
1. Open a new temporary file for writing (OUT).
2. Write the new text to OUT.
3. Close OUT.
4. Append the IN stream to OUT.
5. Rename OUT to IN.
To append IN to OUT (step #4), you could use the File.AppendAllText() method, if the file is a text file.
See below for a sample implementation. It may seem like more code, but ultimately, if the file grows very big, you do not need to load the entire thing into memory first.
-dZ.
Sorry, I just noticed that the initial WriteAllText() call is unnecessary and will open and close the file, wasting resources. I also realized that I left some typos when I changed the name of the variables.
The attached code is corrected. Please note that this is from the top of my head, so I can't guarantee that it will compile, but it should illustrate the technique just the same.
-dZ.
i will discuss a totally alternative approach here... i am going the alternative approach route because if you are going to use a flat file to save comments then how will the demarcation between comments done i.e. when was the comment added and where one comment ends and where the next one begins
the approach that i am going to take is that instead of adding the comments to a flat file add them in an XML file by adding a new comment node and when the comment has been added just publish the comments in the reverse order from the XML
ragi0017, although your approach is sound, I believe that the intention of the author is to have the program work like the old "guestbook" scripts used in the Web of yore; whereas the guestbook content is just a large html block (e.g. a table or a set of div blocks) and every time a new one is added, it is constructed completely and written to the top of the file. Then this file is just rendered verbatim into the output stream. Sort of like a cheap pre-render mechanism.
Of course, this is conjecture on my part, we'll need to hear from the author. There are certainly better approaches to this, but they would probably require more labor in changing the program to support them.
If indeed the author is looking for the best way to store incremental data and display it in LIFO (Last In, First Out) order, then we can certainly offer better alternatives, of which ragi0017's is one.
-dZ.
Business Accounts
Answer for Membership
by: CuteBugPosted on 2009-01-24 at 23:21:06ID: 23459952
Use this code
Select allOpen in new window