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

How to write to the beginning of a file

P: n/a
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 file
and start editing. The form must write to the beginning of the file so
that the update appears on the top of the file instead.

I was actually writing in PHP but see that they do not support it. So
I turned to C++. On About.com, the tutorial on C++ Lesson 12 says that
ios::ate is used to write anywhere in the file without truncating. So
i did a search on how I can manipulate that to the beginning of a file.
I could not find anything.

Is there anyone who can help solve this problem?

Oct 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
an*********@gmail.com wrote:
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 file
and start editing. The form must write to the beginning of the file so
that the update appears on the top of the file instead.

I was actually writing in PHP but see that they do not support it. So
I turned to C++. On About.com, the tutorial on C++ Lesson 12 says that
ios::ate is used to write anywhere in the file without truncating. So
i did a search on how I can manipulate that to the beginning of a file.
I could not find anything.

Is there anyone who can help solve this problem?


You need to be clear, do you want to insert something at the beginning
of a file preserving all the old content, or to you want to write
something at the beginning of a file overwriting what is already at the
beginning of a file.

The first is impossible, the second is not.

I guess you want the first. Because it is impossible you need to do
something different. Write what you want to a new file, then copy
everything from the old file to the new file, then delete the old file,
then rename the new file to the same name as the old file.

If you want the second then use

file.seekp(0)

to move to the beginning of a file.

John
Oct 15 '05 #2

P: n/a
I want the to insert at the beginning of the file preserving the rest
of the content.

I really did not want to hear the word impossible but thank you for
your input.

Nas

Oct 15 '05 #3

P: n/a

<an*********@gmail.com> schreef in bericht
news:11*********************@g49g2000cwa.googlegro ups.com...
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 file
and start editing. The form must write to the beginning of the file so
that the update appears on the top of the file instead.

I was actually writing in PHP but see that they do not support it. So
I turned to C++. On About.com, the tutorial on C++ Lesson 12 says that
ios::ate is used to write anywhere in the file without truncating. So
i did a search on how I can manipulate that to the beginning of a file.
I could not find anything.

Is there anyone who can help solve this problem?

Hi,

Write the new data to a new file. Append the data from the old file to the
new file. Copy back the new file to the old file.

John
Oct 16 '05 #4

P: n/a
> I was actually writing in PHP but see that they do not support it.
Actualy, you are wrong. You can do this in PHP pretty easily.
here's how you do it with function file_get_contents
(http://www.php.net/file_get_contents):
<?php
// variable $new is the text to be inserted at the begining of file.
// file 'changes.txt' is on the same folder as the script.

// read the entire file into variable $old_file_contents
$old_file_contents = file_get_contents('changes.txt');
// put the new text and old text into variable $new_file_contents
$new_file_contents = $new . $old_file_contents;
// (over)write to the file
$file = fopen('changes.txt', 'w');
if ($file)
{
fwrite ($file, $new_file_contents);
fclose ($file);
}
?>

Same algorythm (with different functions) could also be implemented in
C++. Good luck!

Oct 16 '05 #5

P: n/a
Thanks a lot for the help from everyone...

What i ended up doing was this

1) submit form data to 'new file'
2) append data from 'update file' to end of 'new file'
3) rename 'new file' to 'update file'
-if there is an error, you could use copy() feature
instead... again this is in PHP

Thanks a lot for the ideas.

Oct 18 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.