copying in files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 1
Reputation: vcgillu is an unknown quantity at this point 
Solved Threads: 0
vcgillu vcgillu is offline Offline
Newbie Poster

copying in files

 
0
  #1
Mar 20th, 2006
hi.........

to every one,

actually i'm learning c/c++.

i'm struck in the file copying topic.

my problem is

appending file1 to file2 ,

i.e we should be able to append file1 anywhere in file2 as per our wish.


can anyone help me out regarding this with some coding part or pseudo code.


thanking u

gillu
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,614
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: copying in files

 
0
  #2
Mar 20th, 2006
>we should be able to append file1 anywhere in file2
Append? Or insert? Appending always adds new data to the end of the file, but inserting can add new data anywhere in the file. Appending is easy because C and C++ support an open mode for files that strictly appends:
  1. open input for reading
  2. open output for appending
  3.  
  4. while there's another line in input
  5. write line to output
  6.  
  7. close output
  8. close input
  9.  
Inserting is harder because, unless you use record oriented files, you can't actually insert into a file. You need to use an intermediate working file to copy the first chunk of the output file, then the input file, and then the second chunk of the output file, just like inserting a substring into a C-style string:
  1. open input for reading
  2. open output for reading
  3. open scratch for writing
  4.  
  5. while not at the insertion point in output
  6. write line to scratch
  7.  
  8. while there's another line in input
  9. write line to scratch
  10.  
  11. while there's another line in output
  12. write line to scratch
  13.  
  14. close input
  15. reopen output for writing
  16. reopen scratch for reading
  17.  
  18. while there's another line in scratch
  19. write line to output
  20.  
  21. remove scratch
  22. close output
  23.  
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC