The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to insert text in the middle of a file kartikkumar84@g UNIX for Dummies Questions & Answers 6 05-10-2008 11:35 AM
insert text in the middle of a file relle Shell Programming and Scripting 3 03-13-2008 12:37 PM
How to insert the 1st arg into the middle of the file boris Shell Programming and Scripting 4 04-12-2007 09:21 PM
How to insert text into first line of the file and middle of the file? ali hussain Shell Programming and Scripting 3 03-05-2007 02:54 AM
insert text into the middle of a original file mopimp Shell Programming and Scripting 1 03-26-2006 07:11 AM

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 10-04-2004
Registered User
 

Join Date: Sep 2004
Posts: 13
Question how to insert and delete characters in the middle of file

I have a problem that I want to insert and delete some chars in the middle of a file. fopen() and fdopen() just allow to append at the end.

Is there any simple method or existing library that allow these actions? Thanks in advance.
Reply With Quote
Forum Sponsor
  #2  
Old 10-04-2004
Registered User
 

Join Date: Sep 2004
Posts: 13
Isn't there any other clever and efficient way to do that?
Reply With Quote
  #3  
Old 10-05-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,272
Driver gave you the clever efficient way.
Reply With Quote
  #4  
Old 10-06-2004
Registered User
 

Join Date: Sep 2004
Posts: 13
anyway, thanks veru much
Reply With Quote
  #5  
Old 10-07-2004
mbb mbb is offline
Registered User
 

Join Date: Aug 2001
Location: UK
Posts: 103
If you have the memory, you might want to read the whole file into memory, manipulate it there and then dump the results back to disk.

That could be faster depending on a number of factors e.g number of search operations, insert/delete operations, disk performance or size of file.
__________________
Senior Analyst/Programmer
Reply With Quote
  #6  
Old 10-07-2004
Registered User
 

Join Date: Sep 2004
Posts: 13
Quote:
If you have the memory, you might want to read the whole file into memory, manipulate it there and then dump the results back to disk.

That could be faster depending on a number of factors e.g number of search operations, insert/delete operations, disk performance or size of file.
But this method of loading all the data and dumping the useful data back to the file works like putting the data to a newly created file. My question is whether there exist a more efficient library for me to insert/delete characters in any position of a file. Anyway, thanks for your response.
Reply With Quote
  #7  
Old 10-07-2004
mbb mbb is offline
Registered User
 

Join Date: Aug 2001
Location: UK
Posts: 103
Quote:
Originally posted by Driver
> If you have the memory, you might want to read the whole file into memory, manipulate it there and then dump the results back to disk.

Well, if one really wants to modify it in memory, one could also mmap() the file. Using MAP_SHARED you won't need any memory to back the file, as it is already done by the disk. memmove() to move data to make room/overwrite areas of the file and ftruncate() to grow/shrink the file. But I do not recommend this because it likely does not buy anything and may well be slower, requires more effort and decreases the degree of portability.
Yes, I agree, which is why I say "could". A lot depends on machine specs and the problem itself. The problem described is quite simple, but a mail merge application, indexing or sorting application will require due consideration to performance.

My point to the OP was quite simple and old fashioned: memory is faster than disk!

( I hope that is still true these days )

Peace
__________________
Senior Analyst/Programmer
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 05:47 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0