473,465 Members | 1,747 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Writing at the beginning of a file

Let's say I already wrote a file and have the following:

testing
testing testing
testing testing testing

Is there an easy way to write something of variable length at the top
of the file?

For example,

6 testing written
testing
testing testing
testing testing testing

I tried to write some garbage on top right after opening the file and
then use seek to overwrite the garbage, but since the string to be
written can be of variable length, I'm not sure how much garbage I have
to write initially.

The other way to do what I want is to write the whole thing to a new
file, but I want to skip that method if there's an alternative way.

Another way of doing it is to buffer the whole file writing into some
variable, but that means I have to change 2000+ lines of codes and
change fp.write() to something else.

Any suggestions please?

Thanks
Thierry

Sep 14 '05 #1
4 1798
Thierry -

Check out the StringIO module. It will allow you to buffer the whole
file into a string, and then give you a pseudo file pointer to the
string buffer, so that your "fp.write"s will work unchanged.

-- Paul

Sep 14 '05 #2
On 2005-09-14, Thierry Lam <la********@gmail.com> wrote:
Let's say I already wrote a file and have the following:

testing
testing testing
testing testing testing

Is there an easy way to write something of variable length at the top
of the file?
No.

[...]
The other way to do what I want is to write the whole thing to a new
file, but I want to skip that method if there's an alternative way.
There isn't.
Another way of doing it is to buffer the whole file writing into some
variable, but that means I have to change 2000+ lines of codes and
change fp.write() to something else.

Any suggestions please?


Looks like you've figured it out already.

--
Grant Edwards grante Yow! Are you selling NYLON
at OIL WELLS?? If so, we can
visi.com use TWO DOZEN!!
Sep 14 '05 #3
On 14 Sep 2005 07:13:50 -0700, "Thierry Lam" <la********@gmail.com> wrote:
Let's say I already wrote a file and have the following:

testing
testing testing
testing testing testing

Is there an easy way to write something of variable length at the top
of the file?

For example,

6 testing written
testing
testing testing
testing testing testing

I tried to write some garbage on top right after opening the file and
then use seek to overwrite the garbage, but since the string to be
written can be of variable length, I'm not sure how much garbage I have
to write initially.

The other way to do what I want is to write the whole thing to a new
file, but I want to skip that method if there's an alternative way.

Another way of doing it is to buffer the whole file writing into some
variable, but that means I have to change 2000+ lines of codes and
change fp.write() to something else.

Any suggestions please?

Maybe avoid writing at the beginning?

Consider whether appending suitably tagged info at the end of the
file might work for your actual application. If the file-using program
knows of this structure, it can seek e.g. to the end minus a fixed 4 bytes
and read those, assuming that will an n-digit (or maybe rfind-delimited) ascii
number offset to seek back to the beginning of the variable-length chunk
you appended. Then what you read forward from there can be used as if
prefixed to the whole file (or contain fixup info for other parts of
the file before use). This can be a handy way of incrementally modifying
a file without rewriting the whole. It all depends, but it may be an option ;-)

For real safety though, you don't modify or delete an existing important
file until you know you have a new representation safely completed. I say
"new representation" since that also covers the possiblity of original plus
diff patch file as separate files, which could also be an option.

Regards,
Bengt Richter
Sep 15 '05 #4

"Thierry Lam" <la********@gmail.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
Is there an easy way to write something of variable length at the top
of the file? .... The other way to do what I want is to write the whole thing to a new
file, but I want to skip that method if there's an alternative way.
Grant has already given you the two no's to the questions you asked.
Any suggestions please?


A slightly different question "How do I write a variable length file
summary after writing the data so that the summary is easy to read without
reading the entire file, and so I can add data later?" has a yes answer:

1. reserve a fixed amount of space at the top for a 'pointer' to the
summary.
2. write the data
3. write the summary
4. rewind (seek to beginning) and write the 'pointer'.

The pointer can be either the offset from the beginning or the offset from
the end (= length of summary) in either binary or text format.

To read, read the pointer, seek to appropriate place, and read summary.

To add data:
1. read the summary
2. write more data, starting where the old summary started
3. write revised summary and pointer.

Terry J. Reedy

Sep 15 '05 #5

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

Similar topics

3
by: Mahesha | last post by:
Hello, I'm new to C++ and I have requirement to open a existing text file in write mode and write 2 new lines of text in the beginning of the file. I'm working with fstream standard library. If...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
10
by: Magnus Lycka | last post by:
We're using DOM to create XML files that describes fairly complex calculations. The XML is structured as a big tree, where elements in the beginning have values that depend on other values further...
1
by: tim | last post by:
Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi file then after doing that, put some controllers...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
3
by: Rajorshi Biswas | last post by:
Hi all, I'm aware that this might not be a "C-specific" question, if so, please let me know which group is the most appropriate for this kind of question. This is a question on unix...
2
by: totoro2468 | last post by:
Here is my code and output. Why is it writing to the array incorrectly, when I rewinded the file to the beginning? CODE: void ReadString (char *filename, int *lengthPtr) { FILE *ifp; char...
1
by: Twistfactory | last post by:
Hi all, I'm trying to implement a voice recording function. Basically it's supposed to record the WAV and write it out to the file, at which point LAME will kick in and convert it to MP3. The...
0
by: Sid Price | last post by:
I am writing a service using VB.NET 2005 and having some trouble getting started. I sued the wizard to create the service and I have added some code to the "OnStart" method. Using the technique...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
1
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...
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,...
0
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...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.