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

>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
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.