C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 4th, 2003, 03:25 AM
abhishek abhishek is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 abhishek User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Question Appending data

How we can append data in middle of file through c code
ex -original file contents
First line
Second line
now the file contents
Firts line
New line
second line

Reply With Quote
  #2  
Old July 5th, 2003, 05:36 AM
dwise1_aol's Avatar
dwise1_aol dwise1_aol is offline
Contributing User
Dev Shed Expert (3500 - 3999 posts)
 
Join Date: Jan 2003
Location: USA
Posts: 3,977 dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level)dwise1_aol User rank is Colonel (50000 - 60000 Reputation Level) 
Time spent in forums: 1 Month 4 Days 5 h 26 m 2 sec
Reputation Power: 558
You mean: Inserting data into the middle of a file

Of course, it all depends on how you have organized your file. For example, some file formats would index its records so that new records are appended to the end of the file and the index would report that new record's position among the other records. Of course, such formats can be complex.

Rather, I assume that you want to insert new data into the middle of a simple sequential file. Try to think about it as you would insert a new item into the middle of an array: everything that will come after the new data needs to be moved to the right one position. You effectively need to do the same thing to the file.

One approach would be:
1. Open a temporary file.
2. Copy from the original file to the new file every line that precedes the new line.
3. Write the new line to the new file.
4. Write the rest of the original file to the new file.
5. Delete the original file.
6. Rename the temporary file to the original file's name.

For most smaller files, that should work fine. If the file becomes too large, then you may need to turn to a more complex file format.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Appending data


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
Stay green...Green IT