The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 02:35 PM
insert text in the middle of a file relle Shell Programming and Scripting 3 03-13-2008 03:37 PM
How to insert the 1st arg into the middle of the file boris Shell Programming and Scripting 4 04-13-2007 12:21 AM
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 05:54 AM
insert text into the middle of a original file mopimp Shell Programming and Scripting 1 03-26-2006 10:11 AM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rating: Thread Rating: 1 votes, 5.00 average. Display Modes
  #1 (permalink)  
Old 10-04-2004
ivancheung ivancheung is offline
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.
  #2 (permalink)  
Old 10-04-2004
ivancheung ivancheung is offline
Registered User
  
 

Join Date: Sep 2004
Posts: 13
Isn't there any other clever and efficient way to do that?
  #3 (permalink)  
Old 10-05-2004
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,717
Driver gave you the clever efficient way.
  #4 (permalink)  
Old 10-06-2004
ivancheung ivancheung is offline
Registered User
  
 

Join Date: Sep 2004
Posts: 13
anyway, thanks veru much
  #5 (permalink)  
Old 10-07-2004
mbb mbb is offline
Registered User
  
 

Join Date: Aug 2001
Location: UK
Posts: 104
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.
  #6 (permalink)  
Old 10-07-2004
ivancheung ivancheung is offline
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.
  #7 (permalink)  
Old 10-07-2004
mbb mbb is offline
Registered User
  
 

Join Date: Aug 2001
Location: UK
Posts: 104
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
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 08:19 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0