Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coping text from one file to another

Status
Not open for further replies.

bdsopt

Technical User
May 31, 2002
10
US
Hello:

Does anybody have an example for copying text from one file and appending it to another file? For example:

Open File1
Open File2
copy text from File2
Append text from File2 to File1
save and close File2
save and close File1

Thanks

 
Source for appending File1 to File2. Hope This helps

string File1 = "x:\file1" ; Full path and name of source
string File2 = "x:\file2" ; Full path and name of target
string LineToCopy ; String used for line to copy

proc main ; Start main procedure
fopen 1 File1 read text ; Open source file
fseek 1 0 0 ; Go to start of source
fopen 2 File2 append text ; Open target file
fseek 2 0 2 ; Go to end of target
while not feof 1 ; Loop while reading source
fgets 1 LineToCopy ; Extract line from source
fputs 2 LineToCopy ; Insert line into target
endwhile ; End Loop
fclose 2 ; Save and close target file
fclose 1 ; Close source file
endproc ; End main procedure


 
Mr Noisy's script is correct, but you do not need the fseek commands. The fopen will automatically put the pointer at the beginning of file1 and and append command in the file2 fopen statement is enough to put the additional lines at the end. So the shorter version would be:
[tt]
string File1 = "x:\file1" ; Full path and name of source
string File2 = "x:\file2" ; Full path and name of target
string LineToCopy ; String used for line to copy

proc main ; Start main procedure
fopen 1 File1 read text ; Open source file
fopen 2 File2 append text ; Open target file
while not feof 1 ; Loop while reading source
fgets 1 LineToCopy ; Extract line from source
fputs 2 LineToCopy ; Insert line into target
endwhile ; End Loop
fclose 2 ; Save and close target file
fclose 1 ; Close source file
endproc ; End main procedure [/tt] Robert Harris
 
Thank you. This is exactly what I needed.

Brad

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top