textfile - write to beginning of file?
textfile - write to beginning of file?
am 23.01.2007 16:58:56 von 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
Re: textfile - write to beginning of file?
am 23.01.2007 17:10:01 von Anthony Jones
"btopenworld" wrote in message
news:2bidnXwnurq7rSvYnZ2dnUVZ8qugnZ2d@bt.com...
> 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.
Re: textfile - write to beginning of file?
am 23.01.2007 17:35:27 von 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
"Anthony Jones" wrote in message
news:OpOKxjwPHHA.1604@TK2MSFTNGP05.phx.gbl...
>
> "btopenworld" wrote in message
> news:2bidnXwnurq7rSvYnZ2dnUVZ8qugnZ2d@bt.com...
> > 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.
>
>
>
>
>
Re: textfile - write to beginning of file?
am 23.01.2007 18:26:23 von Tim Slattery
"btopenworld" wrote:
>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.
--
Tim Slattery
MS MVP(DTS)
Slattery_T@bls.gov
http://members.cox.net/slatteryt
Re: textfile - write to beginning of file?
am 23.01.2007 18:54:35 von 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
"Tim Slattery" wrote in message
news:p8hcr2tp8jj5vohajvdmri2lhpck4itp3k@4ax.com...
> "btopenworld" wrote:
>
> >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.
>
> --
> Tim Slattery
> MS MVP(DTS)
> Slattery_T@bls.gov
> http://members.cox.net/slatteryt
Re: textfile - write to beginning of file?
am 02.02.2007 06:42:38 von Roland Hall
"btopenworld" wrote in message
news:WsadnQdAIP_a1ivYnZ2dnUVZ8tGqnZ2d@bt.com...
> 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.
--
Roland Hall
Re: textfile - write to beginning of file?
am 02.02.2007 07:51:30 von exjxw.hannivoort
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
> "btopenworld" wrote in message
> news:WsadnQdAIP_a1ivYnZ2dnUVZ8tGqnZ2d@bt.com...
>> 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 need to use a new file or rename. Not elegant but a few steps
> shorter. I agree with the XML or database suggestion.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Re: textfile - write to beginning of file?
am 02.02.2007 08:01:52 von Roland Hall
"Evertjan." wrote in message
news:Xns98CB4FF1515D3eejj99@194.109.133.242...
> Roland Hall wrote on 02 feb 2007 in
> microsoft.public.inetserver.asp.general:
>
>> "btopenworld" wrote in message
>> news:WsadnQdAIP_a1ivYnZ2dnUVZ8tGqnZ2d@bt.com...
>>> 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.
--
Roland Hall
Re: textfile - write to beginning of file?
am 02.02.2007 10:23:08 von mmcginty
"Roland Hall" wrote in message
news:e7BoxfpRHHA.4088@TK2MSFTNGP06.phx.gbl...
> "Evertjan." wrote in message
> news:Xns98CB4FF1515D3eejj99@194.109.133.242...
>> Roland Hall wrote on 02 feb 2007 in
>> microsoft.public.inetserver.asp.general:
>>
>>> "btopenworld" wrote in message
>>> news:WsadnQdAIP_a1ivYnZ2dnUVZ8tGqnZ2d@bt.com...
>>>> 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
> --
> Roland Hall
>
>
Re: textfile - write to beginning of file?
am 02.02.2007 11:34:56 von Anthony Jones
"btopenworld" wrote in message
news:Au-dneGxDtEkpSvYnZ2dnUVZ8qaqnZ2d@bt.com...
> 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.
Re: textfile - write to beginning of file?
am 02.02.2007 16:27:45 von exjxw.hannivoort
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.
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Re: textfile - write to beginning of file?
am 02.02.2007 20:12:23 von Roland Hall
"Mark J. McGinty" wrote in message
news:OFPpDvqRHHA.5064@TK2MSFTNGP02.phx.gbl...
>
> "Roland Hall" wrote in message
> news:e7BoxfpRHHA.4088@TK2MSFTNGP06.phx.gbl...
>> "Evertjan." wrote in message
>> news:Xns98CB4FF1515D3eejj99@194.109.133.242...
>>> Roland Hall wrote on 02 feb 2007 in
>>> microsoft.public.inetserver.asp.general:
>>>
>>>> "btopenworld" wrote in message
>>>> news:WsadnQdAIP_a1ivYnZ2dnUVZ8tGqnZ2d@bt.com...
>>>>> 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.
--
Roland Hall
Re: textfile - write to beginning of file?
am 03.02.2007 00:16:34 von Bob Lehmann
I still find it incredulous that not everyone drives a Corvette or Cadillac,
or has a swimming pool.
Bob Lehmann
"Roland Hall" wrote in message
news:e40363vRHHA.1364@TK2MSFTNGP06.phx.gbl...
> "Mark J. McGinty" wrote in message
> news:OFPpDvqRHHA.5064@TK2MSFTNGP02.phx.gbl...
> >
> > "Roland Hall" wrote in message
> > news:e7BoxfpRHHA.4088@TK2MSFTNGP06.phx.gbl...
> >> "Evertjan." wrote in message
> >> news:Xns98CB4FF1515D3eejj99@194.109.133.242...
> >>> Roland Hall wrote on 02 feb 2007 in
> >>> microsoft.public.inetserver.asp.general:
> >>>
> >>>> "btopenworld" wrote in message
> >>>> news:WsadnQdAIP_a1ivYnZ2dnUVZ8tGqnZ2d@bt.com...
> >>>>> 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.
>
> --
> Roland Hall
>
>
Re: textfile - write to beginning of file?
am 03.02.2007 00:19:05 von Trevor_L.
Bob Lehmann wrote:
> 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/
----------------------------------------
Re: textfile - write to beginning of file?
am 03.02.2007 00:41:42 von Roland Hall
"Bob Lehmann" wrote in message
news:OVFcJ%23xRHHA.4832@TK2MSFTNGP04.phx.gbl...
>I still find it incredulous that not everyone drives a Corvette or
>Cadillac,
> or has a swimming pool.
Amazing, isn't it?
--
Roland Hall
Re: textfile - write to beginning of file?
am 03.02.2007 17:48:49 von 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
"Roland Hall" wrote in message
news:u%23NkYOyRHHA.4744@TK2MSFTNGP02.phx.gbl...
> "Bob Lehmann" wrote in message
> news:OVFcJ%23xRHHA.4832@TK2MSFTNGP04.phx.gbl...
> >I still find it incredulous that not everyone drives a Corvette or
> >Cadillac,
> > or has a swimming pool.
>
> Amazing, isn't it?
>
> --
> Roland Hall
>
>
Re: textfile - write to beginning of file?
am 04.02.2007 20:44:55 von 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" wrote in message
news:%23vy0JK7RHHA.4076@TK2MSFTNGP05.phx.gbl...
> Yes. I would guess that these slackers are probably blowing their money on
> things like food, or shoes for their kids.
>
> Bob Lehmann
>
> "Roland Hall" wrote in message
> news:u%23NkYOyRHHA.4744@TK2MSFTNGP02.phx.gbl...
>> "Bob Lehmann" wrote in message
>> news:OVFcJ%23xRHHA.4832@TK2MSFTNGP04.phx.gbl...
>> >I still find it incredulous that not everyone drives a Corvette or
>> >Cadillac,
>> > or has a swimming pool.
>>
>> Amazing, isn't it?
>>
>> --
>> Roland Hall
>>
>>
>
>