Example 95: File Copying

Learn how to copy the contents of one file to another.

We'll cover the following


Write a program that receives source filename and target filename as command-line arguments and then copies contents of the source file into the target file using low-level disk I/O functions.

Try it yourself

Try to solve this question on your own in the code widget below. If you get stuck, you can always refer to the solution provided.

Note: To run your solution or our given solution, follow the following steps:

  • Click on the run button.
  • Wait for the 4 steps to complete, and then switch to the terminal tab.
  • In the terminal tab, write the following command:
    gcc main.c -o main
    Follow it with this command:
    ./main main.c newmain.c
    The above command will copy the contents from main.c into a new file named newmain.c.
    To view the contents of the newmain.c file, enter the following command:
    cat newmain.c

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.