473,406 Members | 2,713 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.

Editing Files: Help!

Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).

How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?

Jul 31 '07 #1
3 1167
Rik
On Tue, 31 Jul 2007 03:10:10 +0200, ttrium <ar*****@gmail.comwrote:
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).

How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?
You cannot 'prepend' data. You'll have to fetch the content, write your
new content to the beginning of the file, and then add the old content.
--
Rik Wasmus
Jul 31 '07 #2
On Jul 30, 8:15 pm, Rik <luiheidsgoe...@hotmail.comwrote:
On Tue, 31 Jul 2007 03:10:10 +0200, ttrium <arco...@gmail.comwrote:
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).
How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?

You cannot 'prepend' data. You'll have to fetch the content, write your
new content to the beginning of the file, and then add the old content.
--
Rik Wasmus
Is there a simpler way by using databases?

Jul 31 '07 #3
ttrium wrote:
Ok, so this is probably the easist question ever posted, but I
honestly can't find the answer (if there is one).

How do I (by which function) add data to a file at the beginning of a
file, rather than the end of the file (as is with fopen(file, "a"))?
Two ways:

1. Open the file, read it all into memory, truncate the file, write the
new data followed by the old data.

2. Open the file, open a temporary file, write the new data to the
temporary file, copy the data from the original file to the temporary
file, close both files. delete the original file and rename the
temporary file.

#2 is obviously more work, but it doesn't require lots of memory to read
a large file into memory, and won't lose the file if the system crashes
in the middle.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================
Jul 31 '07 #4

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

Similar topics

4
by: Jerry Khoo | last post by:
i am currently working on an assignment that requires the program to manage the files like editing file content, modifying file content, and deleting file contents. My question is what sort of...
1
by: sam.collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
8
by: Sam Collett | last post by:
Is there a basic guide on Xml document creation and editing (simpler than the MSDN docs). Say I want to create a file containing the following: <?xml version="1.0" encoding="utf-8"...
12
by: Ron Weldy | last post by:
I have a test server runinng 2003/IIS 6 with a mixture of asp and asp.net files. On my workstation I have a share set up to the folder where the web files reside. I am just doing quick and dirty...
0
by: Vanga Sasidhar | last post by:
Hi All, Please help me in the following problem. I am having some files with AVI extension. I want to make two programs in Visual Basic .NET which will work with these AVI Files. One program...
12
by: Thomas Bartkus | last post by:
Does anyone use emacs together with both WordStar key bindings and python mode? I'm afraid that Wordstar editing key commands are burned R/O into my knuckles! I would like to play with emacs...
4
by: genojoe | last post by:
My development computer (1 gig, XP Home Edition SP2) freezes when I am editing a very large VB.NET project. It can take minutes to move between a code pane and the Find and Replace pane. On...
1
by: =?Utf-8?B?TmV0aGVzaA==?= | last post by:
Where I can find to download "Manifest Generation and Editing Tool (Mage.exe) " or "Manifest Generation and Editing Tool, Graphical Client (MageUI.exe) ". Is there any other tool which will help...
1
by: zivon | last post by:
now for the bigger problam :) I know you pepole hate using OE for sending emails, but its user friendly and its needed in this case... I found on this forum, a code that sends email using OE...
7
by: JeremyWoertink | last post by:
I have an app that I need to make changes to, and on my local copy I have a start.aspx.vb file that I edited and tested, now when I go to the app on the server, it doesn't show any of the .vb...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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,...

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.