472,356 Members | 708 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,356 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 17503
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
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
1
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...

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.