473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to write to the beginning of a file

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
5 17883
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
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

<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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Google Mike | last post by:
I have PHP version 4.2.2 that ships with RH9. I want to have it write to a file like this: function WriteLog($sLogFile, $sMsg) { if (substr($sLogFile,1,1) != '/') { $sLogFile =...
6
by: Tony C | last post by:
Does Python have a function that is analogous to C's write() or fwrite()- that is , I want to write a file (of arbitrary data) that is 100K, or 1MB (or more) bytes long.. Both write() and...
8
by: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
1
by: Jim Heimer | last post by:
Hi, I've been using schema.ini to read an ascii file to a dataset. Would it be possible to write to the ascii file the same way I read from it? In my case, the initial ascii file has two...
0
by: chipotle_pickle | last post by:
You're letting a lot of bad links creep onto that page. Do a Google search for "genesis world energy". The second item found is a broken link to the newsroom. (The third item found explains how the...
2
by: ykgoh | last post by:
Hi. I've a problem of being able to create and remove a directory but unable to write a file inside the created directory for some strange reason. I suspect that this problem could be vaguely...
3
by: David Thielen | last post by:
Hi; I created a virtual directory in IIS 6.0 and my asp.net app runs fine. But when it tries to write a file I get: Access to the path is denied. - C:\Inetpub\wwwroot\RunReportASP\images ...
2
by: Bina | last post by:
hi, I want to read & write a html file which contain the japanese character. Now how i will do it?I can read & write english character . but when i read & write Japanese character from the html...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
6
by: Thanh-Nhan Le | last post by:
Hello, I have an ASP application on IIS server: http://localhost/myApp I use ASP and a my own VB activex DLL to create a pdf file and write this file to a subfolder of the Application folder:...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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
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
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...

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.