textfile - write to beginning of file?

B

btopenworld

Hi - question from a relative asp novice

I have written to text files in the past, but always appending new data to
the end of the text file. I now want to add the new data to the beginning of
the file.

The text file would be a record of orders placed through a web form. The
text file would merely be a record of the orders but it needs to be in
descending date order - each new record added to the beginning.

Thanks in advance for any suggestions as to how this is achieved.

John
 
A

Anthony Jones

btopenworld said:
Hi - question from a relative asp novice

I have written to text files in the past, but always appending new data to
the end of the text file. I now want to add the new data to the beginning of
the file.

The text file would be a record of orders placed through a web form. The
text file would merely be a record of the orders but it needs to be in
descending date order - each new record added to the beginning.

Thanks in advance for any suggestions as to how this is achieved.

John

Is there a reason you are not using a DB for this?

If so then XML would be a better solution.

A text file is very limiting.
 
B

btopenworld

Hi Anthony

I wasn't going to use a DB because it didn't seem worth it. The only reason
for the file is because several people within an organisation can place
orders and each needs to check that some-one else hasn't recently ordered
the same item for the firm.

I assumed that I would have the same problem writing to an XML file as it is
really only a text file but perhaps I am approaching this wrongly.

Thanks very much for your interest and help.

John
 
T

Tim Slattery

btopenworld said:
Hi - question from a relative asp novice

I have written to text files in the past, but always appending new data to
the end of the text file. I now want to add the new data to the beginning of
the file.

Can't be done. You'll have to create a new file, write the new stuff
to it, read the old file and put that data on the new file. Then
delete the old file, and rename the new one.
 
B

btopenworld

OK - thanks Tim - at least I know there isn't an easy way - I was concerned
that I might be missing something obvious.

Thanks again

John
 
R

Roland Hall

btopenworld said:
OK - thanks Tim - at least I know there isn't an easy way - I was
concerned
that I might be missing something obvious.

You can't preprend but you can open the file and read it into a variable or
array.
Close and then just overwrite the file with the new line, appending the
var/array to it.
No need to use a new file or rename. Not elegant but a few steps shorter.
I agree with the XML or database suggestion.
 
E

Evertjan.

Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
You can't preprend but you can open the file and read it into a
variable or array.
Close and then just overwrite the file with the new line,

Is appending [to the end] any different,
except that the system does it for you?
 
R

Roland Hall

Evertjan. said:
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
You can't preprend but you can open the file and read it into a
variable or array.
Close and then just overwrite the file with the new line,

Is appending [to the end] any different,
except that the system does it for you?
appending the var/array to it.

??

No. As you write to the file, each will be at the end. I suggested reading
the current file into a variable/array and then close. Open a the file to
overwrite and write the new line and then the variable/array. It would be
different if you just opened it ForAppend which would be at the end. Too
bad there's not a prepend.
 
M

Mark J. McGinty

Roland Hall said:
Evertjan. said:
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
OK - thanks Tim - at least I know there isn't an easy way - I was
concerned
that I might be missing something obvious.

You can't preprend but you can open the file and read it into a
variable or array.
Close and then just overwrite the file with the new line,

Is appending [to the end] any different,
except that the system does it for you?
appending the var/array to it.

??

No. As you write to the file, each will be at the end. I suggested
reading the current file into a variable/array and then close. Open a the
file to overwrite and write the new line and then the variable/array. It
would be different if you just opened it ForAppend which would be at the
end. Too bad there's not a prepend.

Think about it from a file system perspective: files must start at
beginning of a cluster. There's no way to prepend an existing file with an
arbitrary amount of data, the best that could be done would be to prepend it
with an entire cluster -- which would surely be possible, if a file system
was written to accept such a directive. But in practice this so often would
be utterly useless, that neither FAT nor NTFS support it.

Adding to the other end is of course another matter, run out of slack space
in the last cluster, add another cluster to the chain.

Point being the exact end of a file is subject to interpretation; the exact
beginning corresponds exactly with the start of a specific cluster.

Sparse files might work-around this to some degree, but it's so much easier
to append the file and process it in reverse, that I doubt very many logging
subsystems use them.

Lastly consider that reading an entire file into a variable, concatenating
it with something else, and rewriting it all back to disk will become memory
intensive as well as i/o intensive in a heartbeat, if the file's maximum
size is unconstrained, or allowed to be large.


-Mark


 
A

Anthony Jones

btopenworld said:
Hi Anthony

I wasn't going to use a DB because it didn't seem worth it. The only reason
for the file is because several people within an organisation can place
orders and each needs to check that some-one else hasn't recently ordered
the same item for the firm.

I assumed that I would have the same problem writing to an XML file as it is
really only a text file but perhaps I am approaching this wrongly.

Thanks very much for your interest and help.

John

Sorry I forgot this thread until others posted to it.

You correct on reflection XML isn't a good choice for a logging style
solution.

Appending to a text file would have concurrency issues, what happens if
some-one else is currently appending to the file at the time another user
wants to? A small append operation probably such a short period of lock on
the file that it won't be important. However using a technique to pre-pend
can significantly increase the period where a conflict could arise. XML
would have this problem.

If you don't want to use a DB then one option is to simply append to the
file but instead of giving readers direct access to file use another ASP
page. This page could read all the text into an array with one element per
entry. The write the contents of the array to the response in reverse
order.

As Mark points out a large file will have implications for memory but of
course if you anticipate this file getting large then a DB solution becomes
'worth it'.

Anthony.
 
E

Evertjan.

Mark J. McGinty wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
Think about it from a file system perspective: files must start at
beginning of a cluster. There's no way to prepend an existing file
with an arbitrary amount of data, the best that could be done would be
to prepend it with an entire cluster -- which would surely be
possible, if a file system was written to accept such a directive.
But in practice this so often would be utterly useless, that neither
FAT nor NTFS support it.

Adding to the other end is of course another matter, run out of slack
space in the last cluster, add another cluster to the chain.

However the begin and end of a file are only definitions.

One could reverse the file byte or word wize, and so append to the
"beginning". If that file is only occasionally read and very regularily
appended, this seems a possibility.
 
R

Roland Hall

Mark J. McGinty said:
Roland Hall said:
Evertjan. said:
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:

OK - thanks Tim - at least I know there isn't an easy way - I was
concerned
that I might be missing something obvious.

You can't preprend but you can open the file and read it into a
variable or array.
Close and then just overwrite the file with the new line,

Is appending [to the end] any different,
except that the system does it for you?

appending the var/array to it.

??

No. As you write to the file, each will be at the end. I suggested
reading the current file into a variable/array and then close. Open a
the file to overwrite and write the new line and then the variable/array.
It would be different if you just opened it ForAppend which would be at
the end. Too bad there's not a prepend.

Think about it from a file system perspective: files must start at
beginning of a cluster. There's no way to prepend an existing file with
an arbitrary amount of data, the best that could be done would be to
prepend it with an entire cluster -- which would surely be possible, if a
file system was written to accept such a directive. But in practice this
so often would be utterly useless, that neither FAT nor NTFS support it.

Adding to the other end is of course another matter, run out of slack
space in the last cluster, add another cluster to the chain.

Point being the exact end of a file is subject to interpretation; the
exact beginning corresponds exactly with the start of a specific cluster.

Sparse files might work-around this to some degree, but it's so much
easier to append the file and process it in reverse, that I doubt very
many logging subsystems use them.

Lastly consider that reading an entire file into a variable, concatenating
it with something else, and rewriting it all back to disk will become
memory intensive as well as i/o intensive in a heartbeat, if the file's
maximum size is unconstrained, or allowed to be large.

My prepend comment was just for content, not actually looking for
overwritable space on the disk prior the current file. At the file system
level, the file itself, the container, actually appends when it gets larger;
contiguous if space is available, otherwise fragmented with a pointer to the
next location and an update on the file length. The content, however, is
adjusted so new data can be prepended within the file. It's similar to
opening a text file, inserting a blank line at the beginning of the file,
filling it with content and resaving the file. The content is prepended but
the file appends and increases in size to the next available area on the
disk.

My comment that too bad there was not a prepend option, was the desire for a
function to handle this automatically so I don't have to write one to
manipulate it myself.

The first response of using another file (hopefully a temporary one) is the
least memory intensive approach.

Being spoiled, I still find it incredulous that not everyone has high speed
Internet and a SQL server.
 
B

Bob Lehmann

I still find it incredulous that not everyone drives a Corvette or Cadillac,
or has a swimming pool.

Bob Lehmann

Roland Hall said:
Mark J. McGinty said:
Roland Hall said:
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:

OK - thanks Tim - at least I know there isn't an easy way - I was
concerned
that I might be missing something obvious.

You can't preprend but you can open the file and read it into a
variable or array.
Close and then just overwrite the file with the new line,

Is appending [to the end] any different,
except that the system does it for you?

appending the var/array to it.

??

No. As you write to the file, each will be at the end. I suggested
reading the current file into a variable/array and then close. Open a
the file to overwrite and write the new line and then the variable/array.
It would be different if you just opened it ForAppend which would be at
the end. Too bad there's not a prepend.

Think about it from a file system perspective: files must start at
beginning of a cluster. There's no way to prepend an existing file with
an arbitrary amount of data, the best that could be done would be to
prepend it with an entire cluster -- which would surely be possible, if a
file system was written to accept such a directive. But in practice this
so often would be utterly useless, that neither FAT nor NTFS support it.

Adding to the other end is of course another matter, run out of slack
space in the last cluster, add another cluster to the chain.

Point being the exact end of a file is subject to interpretation; the
exact beginning corresponds exactly with the start of a specific cluster.

Sparse files might work-around this to some degree, but it's so much
easier to append the file and process it in reverse, that I doubt very
many logging subsystems use them.

Lastly consider that reading an entire file into a variable, concatenating
it with something else, and rewriting it all back to disk will become
memory intensive as well as i/o intensive in a heartbeat, if the file's
maximum size is unconstrained, or allowed to be large.

My prepend comment was just for content, not actually looking for
overwritable space on the disk prior the current file. At the file system
level, the file itself, the container, actually appends when it gets larger;
contiguous if space is available, otherwise fragmented with a pointer to the
next location and an update on the file length. The content, however, is
adjusted so new data can be prepended within the file. It's similar to
opening a text file, inserting a blank line at the beginning of the file,
filling it with content and resaving the file. The content is prepended but
the file appends and increases in size to the next available area on the
disk.

My comment that too bad there was not a prepend option, was the desire for a
function to handle this automatically so I don't have to write one to
manipulate it myself.

The first response of using another file (hopefully a temporary one) is the
least memory intensive approach.

Being spoiled, I still find it incredulous that not everyone has high speed
Internet and a SQL server.
 
T

Trevor L.

Bob said:
I still find it incredulous that not everyone drives a Corvette or
Cadillac, or has a swimming pool.

Yes, amazing isn't it.

But they don't even sell these cars here and on a 300sq m. block we can't
fit a swimming pool.

Oh what bad luck it is to be poor.

:))
--
Cheers,
Trevor L.
[ Microsoft MVP - FrontPage ]
MVPS Website: http://trevorl.mvps.org/
----------------------------------------
 
B

Bob Lehmann

Yes. I would guess that these slackers are probably blowing their money on
things like food, or shoes for their kids.

Bob Lehmann
 
M

Mike Brind

Not me :) I got my priorities right :-D

Well, not strictly true - I send my kids up the chimneys so they they can
earn some money to pay for food and shoes...

Bob Lehmann said:
Yes. I would guess that these slackers are probably blowing their money on
things like food, or shoes for their kids.

Bob Lehmann
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,129
Messages
2,570,769
Members
47,325
Latest member
sloppy-dobby

Latest Threads

Top