>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:
open input for reading
open output for appending
while there's another line in input
write line to output
close output
close input
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:
open input for reading
open output for reading
open scratch for writing
while not at the insertion point in output
write line to scratch
while there's another line in input
write line to scratch
while there's another line in output
write line to scratch
close input
reopen output for writing
reopen scratch for reading
while there's another line in scratch
write line to output
remove scratch
close output
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Offline 11,807 posts
since Sep 2004