LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-17-2005, 09:20 AM   #1
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Rep: Reputation: 15
How to add data at file beginning in C?


Hello! Couldn't you say, how to add data at beginning of a file effectively?
(in C)
 
Old 04-17-2005, 11:04 AM   #2
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
do you have the file created and with existing content??
i.e.:
is your file something like:
Code:
//File A.txt
some bla bla bla...

//EOF A.txt
and you need to insert a string before the "some bla bla bla..."??
 
Old 04-17-2005, 11:05 AM   #3
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Original Poster
Rep: Reputation: 15
Yes
 
Old 04-17-2005, 11:10 AM   #4
purefan
Member
 
Registered: Aug 2003
Location: Sweden
Distribution: Ubuntu 10.04
Posts: 99

Rep: Reputation: Disabled
go here and look for the seekg function:
http://www.cplusplus.com/doc/tutorial/tut6-1.html

it explains it very well there
 
Old 04-17-2005, 11:15 AM   #5
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Original Poster
Rep: Reputation: 15
This is C++
 
Old 04-17-2005, 11:20 AM   #6
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
There's no portable way to open a file for writing and writing to the start of the file (or in the middle) without overwriting previous content. When one wants to add data to another location than the end of the file or modify existing data, you usually open the existing file for reading and a new file for writing. Then you write the data, new and old, in the order you want to new file. When you're done you close both files, delete the old one and rename the new one. This can be done using functions in the C or C++ standard library, so it's portable.
 
Old 04-17-2005, 11:22 AM   #7
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Original Poster
Rep: Reputation: 15
Thanks. I knew about this way, but I thougt that there is some specific FS lowlevel functions
 
Old 04-17-2005, 11:42 AM   #8
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Maybe it can be done using memory-mapped files, but I don't have a working example to show you and it would also make the program unportable, of course. Even if memory-mapped files doesn't buy you anything compared to the method I described above, it will be a good exercise!
 
Old 04-17-2005, 11:48 AM   #9
Nad0xFF
Member
 
Registered: Apr 2005
Location: Russia, Saint-Petersburg
Distribution: Slackware 10
Posts: 109

Original Poster
Rep: Reputation: 15
Couldn't you give me POSIX (or libc) functions for memory mapped files?
(I've done this in windows, but I don't know UNIX analogues).
BTW, I want to have approximately terabyte files
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I write to the beginning of a file? abefroman Programming 1 06-30-2005 11:56 AM
How can I add custom data to my executable bestofmed Programming 3 07-29-2004 03:24 AM
How to delete a file beginning with '-'? zoomzoom Linux - General 2 02-02-2004 10:01 AM
read from file from the beginning? raven Programming 3 12-05-2003 08:32 PM
radius append/add data ethanchic Linux - Software 0 04-16-2003 02:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:06 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration