944,052 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1898
  • C++ RSS
Mar 20th, 2006
0

copying in files

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
vcgillu is offline Offline
1 posts
since Mar 2006
Mar 20th, 2006
0

Re: copying in files

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.  
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ problem...
Next Thread in C++ Forum Timeline: using 5 different variables





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC