473,624 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read and Write the same file

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
5 4852
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
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
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
In article <ma************ *************** ************@py thon.org>,
Ian Clark <tu*****@gmail. comwrote:
May 2 '07 #5
On 2007-05-01, Gabriel Genellina <ga*******@yaho o.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2656
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to replace the text its reversing eg.
22
13236
by: Jason Heyes | last post by:
Does this function need to call eof after the while-loop to be correct? bool read_file(std::string name, std::string &s) { std::ifstream in(name.c_str()); if (!in.is_open()) return false; char c; std::string str;
1
6738
by: cnu | last post by:
My program generates a log file for every event that happens in the program. So, I open the file and keep it open till the end. This is how I open the file for writing: <CODE> public CLogHandler() { this.m_fsLog = new FileStream(strTodaysLogFile, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Read); this.m_swLog = new StreamWriter(this.m_fsLog);
1
4297
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each time after routine process A, the text file is named "mytext.dat" in following format with "#####" as separator. The maximum entries of them is 5. When reaching the fifth entry, it will delete the very first entry.
2
6793
by: VM | last post by:
Is it possible to open an ascii file, read the first 499 lines, overwrite line 500 with the new string, and close the file? All the lines before and after line 500 will be untouched. Thanks.
3
1855
by: Sharon | last post by:
I'm using StreamWriter to write to file and at the same time I'm using the StreamReader to read from the same file. Doing that cause an exception when I'm trying to create the StreamReader when the file is already opened fro writing by the StreamWriter. How can I write and read at the same time to the same file? -- Thanks Sharon G.
5
5097
by: Sumana | last post by:
Hi All, We developed our project on VC++.Net console application to create image of disk and to write the image We are having problem with reading and writing the sector beyond 6GB Disk or Partition we are using ReadFile , WriteFile and setFilePointerEx to read and write the sectors and we are reading/writing 102400 sectors together, even we have reduced the sectors still it is not able to read or write, our program is working fine...
35
11413
by: RyanS09 | last post by:
Hello- I am trying to write a snippet which will open a text file with an integer on each line. I would like to read the last integer in the file. I am currently using: file = fopen("f.txt", "r+"); fseek(file, -2, SEEK_END); fscanf(file, "%d", &c); this works fine if the integer is only a single character. When I get into larger numbers though (e.g. 502) it only reads in the 2. Is there
3
18958
by: nicolasg | last post by:
Hi, I'm trying to open a file (any file) in binary mode and save it inside a new text file. After that I want to read the source from the text file and save it back to the disk with its original form. The problem is tha the binary source that I extract from the text file seems to be diferent from the source I saved. Here is my code: 1) handle=file('image.gif','rb')
1
3922
by: Sachin Garg | last post by:
I have a program which opens a fstream in binary input+output mode, creating the file if it doesn't exists. But writing doesn't works after reading, it must be something obvious that I am not aware of. f.open(filename,ios::in | ios::out | ios::binary | ios::trunc) The program flow is 1) write some data 2) read the data
0
8625
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8336
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8482
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7168
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6111
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5565
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4082
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4177
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1487
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.