473,405 Members | 2,154 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,405 software developers and data experts.

textfile - write to beginning of file?

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
Jan 23 '07 #1
16 2967

"btopenworld" <jo**@siteweave.netwrote in message
news:2b******************************@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.

Jan 23 '07 #2
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" <An*@yadayadayada.comwrote in message
news:Op**************@TK2MSFTNGP05.phx.gbl...
>
"btopenworld" <jo**@siteweave.netwrote in message
news:2b******************************@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.



Jan 23 '07 #3
"btopenworld" <jo**@siteweave.netwrote:
>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)
Sl********@bls.gov
http://members.cox.net/slatteryt
Jan 23 '07 #4
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" <Sl********@bls.govwrote in message
news:p8********************************@4ax.com...
"btopenworld" <jo**@siteweave.netwrote:
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)
Sl********@bls.gov
http://members.cox.net/slatteryt

Jan 23 '07 #5
"btopenworld" <jo**@siteweave.netwrote in message
news:Ws******************************@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
Feb 2 '07 #6
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
"btopenworld" <jo**@siteweave.netwrote in message
news:Ws******************************@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)
Feb 2 '07 #7
"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
>"btopenworld" <jo**@siteweave.netwrote in message
news:Ws******************************@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
Feb 2 '07 #8

"Roland Hall" <nobody@nowherewrote in message
news:e7**************@TK2MSFTNGP06.phx.gbl...
"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
>Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:
>>"btopenworld" <jo**@siteweave.netwrote in message
news:Ws******************************@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


Feb 2 '07 #9

"btopenworld" <jo**@siteweave.netwrote in message
news:Au******************************@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.


Feb 2 '07 #10
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)
Feb 2 '07 #11
"Mark J. McGinty" <mm******@spamfromyou.comwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...
>
"Roland Hall" <nobody@nowherewrote in message
news:e7**************@TK2MSFTNGP06.phx.gbl...
>"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
>>Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:

"btopenworld" <jo**@siteweave.netwrote in message
news:Ws******************************@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
Feb 2 '07 #12
I still find it incredulous that not everyone drives a Corvette or Cadillac,
or has a swimming pool.

Bob Lehmann

"Roland Hall" <nobody@nowherewrote in message
news:e4**************@TK2MSFTNGP06.phx.gbl...
"Mark J. McGinty" <mm******@spamfromyou.comwrote in message
news:OF**************@TK2MSFTNGP02.phx.gbl...

"Roland Hall" <nobody@nowherewrote in message
news:e7**************@TK2MSFTNGP06.phx.gbl...
"Evertjan." <ex**************@interxnl.netwrote in message
news:Xn********************@194.109.133.242...
Roland Hall wrote on 02 feb 2007 in
microsoft.public.inetserver.asp.general:

"btopenworld" <jo**@siteweave.netwrote in message
news:Ws******************************@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


Feb 2 '07 #13
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/
----------------------------------------

Feb 2 '07 #14
"Bob Lehmann" <no****@dontbotherme.zzzwrote in message
news:OV****************@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
Feb 2 '07 #15
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" <nobody@nowherewrote in message
news:u%****************@TK2MSFTNGP02.phx.gbl...
"Bob Lehmann" <no****@dontbotherme.zzzwrote in message
news:OV****************@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


Feb 3 '07 #16
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" <no****@dontbotherme.zzzwrote in message
news:%2****************@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" <nobody@nowherewrote in message
news:u%****************@TK2MSFTNGP02.phx.gbl...
>"Bob Lehmann" <no****@dontbotherme.zzzwrote in message
news:OV****************@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



Feb 4 '07 #17

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

Similar topics

7
by: Hans A | last post by:
I have a textfile "textfile.txt" containing a list of words. There is one word on each line. I want to pick two random lines from this textfile, and I have tried to do something like: //Loading...
1
by: Manfred Schwab | last post by:
Recording messages and print statements in a textfile during program execution. Is there a similar command to redirect errormessages or print statements into a standart asciifile during...
5
by: anas.hashmi | last post by:
I am trying to write to the beginning of a file. The reason: I want to make a form where board webmasters can use it to insert in updates to a webpage without having to go directly into the web...
2
by: Thomas A | last post by:
Hi, I write a string value to a textfile , if the string dont contains swedish Å,Ä and Ö , the file will be written in ANSI. So far so god but.... If the string contains Å or Ä or Ö ,the...
8
by: andreas | last post by:
A textfile can have different formats like ANSI,UTF8, Unicode ... Using streamreader for a textfile can have different results for chars like é,à,è....depending of the fileformat. To solve this...
1
by: Justin Fancy | last post by:
Hi everyone, I have a textfile which I need to read and compare dates. The text file summarizes every time I do an update to an internet site. Sample output is as follows: Copying...
1
by: | last post by:
Hello everybody, i need your help : i have to write some lines coming from a database into a textfile; Everything goes right while the filesize is quite small or medium; But it's different when...
2
by: RaviRajhulk | last post by:
I am trying to read folder and subfolder and print their name into a text file.I am able to read the folder and its content and write it to the textfile but I am not able to read the content of the...
3
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I am trying to loop a Textfile. This Textfile is located on a shared drive and it is used by another process. When I try to read it I get an exeption: The process cannot access the...
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: 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
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.