CJCoding With Joseph
15free / daySign inCreate account

Copy a File

Write a function bool copyFile(const string& src, const string& dst) that copies a text file line by line. Open src with an ifstream; if it cannot be opened, return false without creating dst. Otherwise open dst with an ofstream, write every line of src to it followed by a newline, and return true. Your function prints nothing itself.

The test creates src with the two lines line one and line two. If your function returns true the test reopens dst and prints every line (see Expected Output).

If it returns false the test prints only:
copy failed
This happens when src is a path in a folder that does not exist, such as missing_dir/src.txt.

Do NOT write a main function.

Expected Output:

line one
line two
copy failed

= newline. The output ends with a newline — print one after the last line.

Hints4 available
Topics:
Streamsofstream
1
Tab to indent (Esc then Tab to move on) · Ctrl+Enter to run · Shift+Alt+F to format · Ctrl+Space to expand shortcuts (cout, fori)

Your Output

Run your code to see the output here...

Test Cases

Run your code to see test case results.