473,383 Members | 1,862 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

FILE I/O in reverse order?

Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.

Thanks alot!

Mark
Nov 14 '05 #1
8 3587
Mark wrote:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?


There is no way, using standard C, to append data at the beginning of a
file. If such a method exists, it will be specific to your
implementation and you should ask about it on a group that discusses
your OS or compiler.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #2
"Mark" <ma*************@excite.com> writes:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.


I'd just read the file backwards.
Nov 14 '05 #3
"Mark" <ma*************@excite.com> writes:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.


I'd recommend this alternative. Not only does C not offer a way
to do what you want to do, I can't even think of a good way to do
it on common operating systems with OS-specific methods.
--
"The way I see it, an intelligent person who disagrees with me is
probably the most important person I'll interact with on any given
day."
--Billy Chambless
Nov 14 '05 #4
"Mark" <ma*************@excite.com> wrote:
# Hi, I have an app that needs to maintain a log file of certain events that
# occur. Normally FILE I/O appends new text to the end of a file. However
# one of the requirements of my app is that the most recent log entries need
# to be at the beginning of the file. My initial thought is that the file
# would have to be rewritten every time a new log entry is added. Can anyone
# suggest a better/simpler way?

Most operating systems you're likely to use today don't support anything
but a long string of bytes, and so neither will most C implementations. There
are many packages written in ANSI C that you can get, or write your own;
for example you can use something like Berkely DB and then write with
decreasing keys so that logical organisation is most recent first. However
such files are unlikely to be usable with most other programs.

# An alternative would be to write the log as normal, then use a separate app
# to post-process the log file into the most-recent-first order required.

On systems like unix, if you write the file as character lines beginning with
a timestamp, you can easily rearrange the file order with 'sort -r'. And unless
your files are huge (like hundreds of megabytes), the total wall clock and
cpu time used by a simple minded approach will actually be less than trying
to be clever

--
Derk Gwen http://derkgwen.250free.com/html/index.html
GERBILS
GERBILS
GERBILS
Nov 14 '05 #5
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<e8***************@newsread1.news.pas.earthli nk.net>...
Mark wrote:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?


There is no way, using standard C, to append data at the beginning of a
file. If such a method exists, it will be specific to your
implementation and you should ask about it on a group that discusses
your OS or compiler.

-Kevin


Why not simply output the new data to a tmp file
then read in the old file outputting it to the tmp
file. Then just rename the tmp file to the old file?

This can be done using standard C.

--
nethlek
Nov 14 '05 #6
Derk Gwen wrote:
On systems like unix, if you write the file as character lines beginning with
a timestamp, you can easily rearrange the file order with 'sort -r'. And unless
your files are huge (like hundreds of megabytes), the total wall clock and
cpu time used by a simple minded approach will actually be less than trying
to be clever


Instead of 'sort -r', why not just use tac?

yourprog | tac >outfile
or
yourprog >tmpfile; tac <tmpfile >outfile

Nov 14 '05 #7
Mantorok Redgormor wrote:
Kevin Goodsell <us*********************@neverbox.com> wrote in message news:<e8***************@newsread1.news.pas.earthli nk.net>...
Mark wrote:

Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?


There is no way, using standard C, to append data at the beginning of a
file. If such a method exists, it will be specific to your
implementation and you should ask about it on a group that discusses
your OS or compiler.

-Kevin

Why not simply output the new data to a tmp file
then read in the old file outputting it to the tmp
file. Then just rename the tmp file to the old file?

This can be done using standard C.


Well, yes, you can do that. I meant that there's no way to write output
at the beginning of a file, pushing the old contents toward the end of a
file (like the insert mode of a text editor).

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Nov 14 '05 #8
Mark wrote:
Hi, I have an app that needs to maintain a log file of certain events that
occur. Normally FILE I/O appends new text to the end of a file. However
one of the requirements of my app is that the most recent log entries need
to be at the beginning of the file. My initial thought is that the file
would have to be rewritten every time a new log entry is added. Can anyone
suggest a better/simpler way?

An alternative would be to write the log as normal, then use a separate app
to post-process the log file into the most-recent-first order required.

Thanks alot!

Mark

Is it a candidate that, read the whole file contents to an array, then
reopen a again, write the new contents. Then, append the original one?

I'm not sure whether this is effective. Because I haven't yet read any
example writing in this style. And, normally, *log* file is big and
growing fast. So, if the file were too big, or the operating frequency
were too high, something bad would be expected.

--
Learning is to improve, but not to prove.

Nov 14 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: Erik Andersson | last post by:
Hi! I need to read a file (line-by-line) in reverse order, without reading the whole file into memory first. Is there an easy way of doing this? As in, is there a PHP function I could use? ...
3
by: R. Rajesh Jeba Anbiah | last post by:
Unfortunately, I couldn't find any way to traverse the object array in reverse order. I'd thought there must be a way to do it with for..in loop, but couldn't find anything yet. Could someone...
14
by: ford_desperado | last post by:
Why isn't ALLOW REVERSE SCANS the default? Why do we have to - drop PK - create an index - recreate PK What are the advantages of indexes that do not allow reverse scans?
20
by: sahukar praveen | last post by:
Hello, I have a question. I try to print a ascii file in reverse order( bottom-top). Here is the logic. 1. Go to the botton of the file fseek(). move one character back to avoid the EOF. 2....
4
by: Jeff Rodriguez | last post by:
How would one go about reading a file line by line in reverse? For Example: -- FILE -- 1 2 3 4 5 -- FILE --
10
by: aatish19 | last post by:
1: Write a program that asks the user to input an integer value and print it in a reverse order Sample Output Enter any number 65564 Reverse of 65564 is 46556 2: Write a program that takes a...
2
by: Jonathan Shan | last post by:
Hello everybody, For some strange reason my output to file keeps writing to the top instead of bottom. Here is the code that exhibits the problem: void printfile(int i) { FILE* write;
4
by: Stupid48 | last post by:
I'm trying to do a simple task but can't seem to find a solution. How do I read lines from a text file backwards. i.e. I want to select the last 20 lines of a text file and display them in...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
40
by: KG | last post by:
Could any one tell me how to reverse the bits in an interger?
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.