By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,513 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Read and Write the same file

P: n/a
Ok, so this is the scenario. I need to create a simple, no-frills XML
editor for non-technical users. It doesn't have to do anything fancy,
what I want is a series of text boxes with the text contents of the
elements pre-printed. Then the users can type their changes into the
text boxes and click submit and it will load the changes in. So here
is the problem, this means I need to open the same file as both read
and write. How do I do this? I'm slowly learning the DOM stuff that I
need to know to do this, but this file thing I haven't been able to
find anywhere.

JonathanB

May 1 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
En Tue, 01 May 2007 20:03:28 -0300, JonathanB <do******@gmail.com>
escribió:
Ok, so this is the scenario. I need to create a simple, no-frills XML
editor for non-technical users. It doesn't have to do anything fancy,
what I want is a series of text boxes with the text contents of the
elements pre-printed. Then the users can type their changes into the
text boxes and click submit and it will load the changes in. So here
is the problem, this means I need to open the same file as both read
and write. How do I do this? I'm slowly learning the DOM stuff that I
need to know to do this, but this file thing I haven't been able to
find anywhere.
Open the file for reading; read and parse the document; close the file.
Build form and show to the user.
Get data after user clicks OK.
Build document, open file for writing, write document, close file.

Reading and writing happen on two separate stages, and you dont have to
keep the file open all the time.

--
Gabriel Genellina
May 1 '07 #2

P: n/a
On 1 May 2007 16:03:28 -0700, JonathanB <do******@gmail.comwrote:
Ok, so this is the scenario. I need to create a simple, no-frills XML
editor for non-technical users. It doesn't have to do anything fancy,
what I want is a series of text boxes with the text contents of the
elements pre-printed. Then the users can type their changes into the
text boxes and click submit and it will load the changes in. So here
is the problem, this means I need to open the same file as both read
and write. How do I do this? I'm slowly learning the DOM stuff that I
need to know to do this, but this file thing I haven't been able to
find anywhere.

JonathanB
Well the most straight forward approach is to read data from the file
into memory. Let the user make any changes. Then save the data back to
the file, overwriting the oringinal copy. Now, this only really works
if you're dealing with small files.

Regardless though, you never really need to have a file open for both
reading and writing. Since they occur (so it sounds) at distinct times
during your program flow, just open it twice: the first to read, the
second to write.

Ian
May 1 '07 #3

P: n/a
Well the most straight forward approach is to read data from the file
into memory. Let the user make any changes. Then save the data back to
the file, overwriting the oringinal copy. Now, this only really works
if you're dealing with small files.

Regardless though, you never really need to have a file open for both
reading and writing. Since they occur (so it sounds) at distinct times
during your program flow, just open it twice: the first to read, the
second to write.

Ian
That makes sense. The largest document is >10K, so it shouldn't be a
problem.

May 2 '07 #4

P: n/a
In article <ma***************************************@python. org>,
Ian Clark <tu*****@gmail.comwrote:
May 2 '07 #5

P: n/a
On 2007-05-01, Gabriel Genellina <ga*******@yahoo.com.arwrote:
En Tue, 01 May 2007 20:03:28 -0300, JonathanB <do******@gmail.com>
escribió:
>Ok, so this is the scenario. I need to create a simple, no-frills XML
editor for non-technical users. It doesn't have to do anything fancy,
what I want is a series of text boxes with the text contents of the
elements pre-printed. Then the users can type their changes into the
text boxes and click submit and it will load the changes in. So here
is the problem, this means I need to open the same file as both read
and write. How do I do this? I'm slowly learning the DOM stuff that I
need to know to do this, but this file thing I haven't been able to
find anywhere.

Open the file for reading; read and parse the document; close the file.
Build form and show to the user.
Get data after user clicks OK.
Build document, open file for writing, write document, close file.

Reading and writing happen on two separate stages, and you dont have to
keep the file open all the time.
A safer way to do this is to write the new version to a
temporary file, close the temporary file, then rename it.
There's much less chance of losing data that way -- no matter
when the program or computer crashes, you've got a complete
copy one version or the other.

--
Grant Edwards grante Yow! I am having FUN... I
at wonder if it's NET FUN or
visi.com GROSS FUN?
May 2 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.