← Back to Streams
Question 610
Copy a File
Copy a File
Write a functionbool copyFile(const string& src, const string& dst)that copies a text file line by line. Opensrcwith anifstream; if it cannot be opened, returnfalsewithout creatingdst. Otherwise opendstwith anofstream, write every line ofsrcto it followed by a newline, and returntrue. Your function prints nothing itself. The test createssrcwith the two linesline oneandline two. If your function returns true the test reopensdstand prints every line (see Expected Output). If it returns false the test prints only: copy failed This happens whensrcis a path in a folder that does not exist, such asmissing_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
Sign in to use hintsTopics:
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.