473,761 Members | 3,187 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pretty simple question about fwrite! Please help!

Hello everyone. Put very simply, I need to have the ability to write to
open a .txt file, write to it, and then close it. I know how to do
this, but the part I am not sure how to accomplish is that each time I
open the file to write it, I want to add the in content below the last
content I just added. Example:

<Comments_Secti on>
<comment>
<name>Joe</name>
<comment_text>H ello</comment_text>
</comment>
</Comments_Sectio n>

So, the next time I open the file, I would like to modify it so it
looks like this:

<Comments_Secti on>
<comment>
<name>Joe</name>
<comment_text>H ello</comment_text>
</comment>
<comment>
<name>Chris</name>
<comment_text>G oodbye</comment_text>
</comment>
</Comments_Sectio n>

How do I insert into a certain point in a file, without affecting the
rest of the file? Thank you very much.

Jul 17 '05 #1
4 1494
ge********@gmai l.com said the following on 21/06/2005 18:57:
Hello everyone. Put very simply, I need to have the ability to write to
open a .txt file, write to it, and then close it. I know how to do
this, but the part I am not sure how to accomplish is that each time I
open the file to write it, I want to add the in content below the last
content I just added. Example:

<Comments_Secti on>
<comment>
<name>Joe</name>
<comment_text>H ello</comment_text>
</comment>
</Comments_Sectio n>

So, the next time I open the file, I would like to modify it so it
looks like this:

<Comments_Secti on>
<comment>
<name>Joe</name>
<comment_text>H ello</comment_text>
</comment>
<comment>
<name>Chris</name>
<comment_text>G oodbye</comment_text>
</comment>
</Comments_Sectio n>

How do I insert into a certain point in a file, without affecting the
rest of the file? Thank you very much.


You can't! File-systems don't work that way.

The only way to achieve what you want is to read the file into a string,
edit the string as necessary, and then write the whole thing back out to
the file.

Actually, you don't have to bother reading in and re-writing anything
before your "insertion" point, it's only necessary to do this from the
insertion point onwards.

--
Oli
Jul 17 '05 #2
ge********@gmai l.com wrote:
How do I insert into a certain point in a file, without affecting the
rest of the file? Thank you very much.


You can't, but in this case that is not what you need. YOu just need to
open the file in 'a+' mode, read (fseek) back to the first '<' (fgetc)
and overwrite from there with the new node and append the closing
element again.

A more robust solution would be to actually use a decent XML parser that
lets you append a child node to the root element.

Jul 17 '05 #3
I see. If I only need to read in from the insertion point on, then how
can I select the location for the insertion point. Is this possible?
Basically jumping to a certain point of a file and delcaring that the
insertion point.

Jul 17 '05 #4
On Tue, 21 Jun 2005 13:24:07 -0700, gene.ellis wrote:
I see. If I only need to read in from the insertion point on, then how
can I select the location for the insertion point. Is this possible?
Basically jumping to a certain point of a file and delcaring that the
insertion point.


How do you know where the insertion point is? And how do you know there
isn't another process writing to the file while you are reading it?

You don't!

Unless you have a very good reason for not using databases for data
storeage use one.

You can quite easily generate a ZML file from the contects of a table when
needed.

Jul 17 '05 #5

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

Similar topics

3
6001
by: seia0106 | last post by:
Hello In the course of writing a program. I have to read and write a media file(*.avi/*.mpg/*.wav) to and from memory buffer. I can put the data(from a *.avi file)in buffer successfully but when i try to write data from buffer into a new file, then i can not play that file. A file size of only 4kb is copied and i can not play the file. The size of original file, whose contents were copied into buffer was 996KB. Can anyone please help...
4
2623
by: Andrew Kibler | last post by:
Two sections of code, in the first one fwrite works, in the second one it doesn't (ms VC++) both are identical except in the working one fseek is used twice to set the file pointer, once just before the fwrite. WHY??? Works: fseek(fp, itemloc, SEEK_SET); tmp = ReadLongBE(fp); if (*itemsize == tmp){ printf("\n%X\n",ftell(fp));
23
17854
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName); fprintf(fnew, " %f", array);
5
3060
by: mhk | last post by:
Hi , is there any way to merge three sorted arrays into a sorted file, without using 4th array. i guess 3 way merge sort is the only option but i dont know its algorithem. can anyone tell me algorithm of 3 way merge sort or anyother way to solve this problem. Thank you veryyyyy much.
51
8288
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct code? many thx!
15
3903
by: Suraj Kurapati | last post by:
Hello, I'm having a rather strange bug with this code: for certain values of 'buf', a segmentation fault occurs when 'free(buf)' is followed by an 'fwrite()'. In the program output, there is no error reported by 'perror()' and the file is written correctly. /* returns NULL upon failure, or a calloc()ed data upon success */ char* recv_data(int *bytes_read);
0
362
by: Greg | last post by:
I am continuing writing my simple file system and I have run across the following problem. I am writing a superblock to a certain frame in the file as follows: (&sb is the address of a superblock struct with size < FRAME_SZ * 4) #define FRAME_SZ 512 int frame = 3 ;
3
4530
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
12
5094
by: hemant.gaur | last post by:
I have an application which writes huge number of bytes into the binary files which is just some marshalled data. int len = Data.size(); //arrary size for (int i = 0; i < len; ++i) fwrite(&Data, 1, 1, f); now after running this for long time and pushing millions of bytes, It once misses writing the last byte of fData. Then the further push of bytes is again correct. As i am not using the return value for the
25
15572
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that true? regards, ...ab
0
9538
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9353
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10123
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9788
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
8794
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...
0
6623
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();...
1
3889
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3481
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2765
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.