C Tutorial – Copying a File

In a previous tutorial we already talked about renaming and removing a file. In this C programming language tutorial we look at how to copy a file.

Let’s look at an example:



	#include<stdio.h>

	int copy_file(char *old_filename, char  *new_filename)
	{
		FILE  *ptr_old, *ptr_new;
		errno_t err = 0, err1 = 0;
		int  a;

		err = fopen_s(&ptr_old, old_filename, "rb");
		err1 = fopen_s(&ptr_new, new_filename, "wb");

		if(err != 0)
			return  -1;

		if(err1 != 0)
		{
			fclose(ptr_old);
			return  -1;
		}

		while(1)
		{
			a  =  fgetc(ptr_old);

			if(!feof(ptr_old))
				fputc(a, ptr_new);
			else
				break;
		}

		fclose(ptr_new);
		fclose(ptr_old);
		return  0;
	}

	int  main(void)
	{
		char  filename_src[101], filename_dest[101];

		printf("\nSource file: ");
		gets_s(filename_src, 100);

		printf("\nDestination filename: ");
		gets_s(filename_dest, 100);

		if(copy_file(filename_src, filename_dest) == 0)
			printf("Copy Successful\n");
		else
			fprintf(stderr, "Error during copy!");
	}

First we take a look at main before we look at the function copy_file().

main()

In main() we first make two buffers, a source and destination buffer, that each can hold 100 characters plus ‘\0’. Using the gets_s function (a more secure gets) the program waits for input from the user. The user must input a source file and a destination file. In the “if statement” the function copy_file() is called. The function is either successful or will fail. A message is printed accordingly.

copy_file()

In main the function copy_file() is called with the source filename and the destination file. The two files are then opened. The first file is opened in read only binary mode. The second file is opened in write binary mode. (If the file doesn’t exist the file is created.) For opening the file fopen_s() is used which is a more secure fopen().

After opening some error checking is done. If one of the two fopen_s() functions fails an -1 is returned.

In the “while statement” the content of the source file is read one byte at the time.
This byte is written to the destination file. This will continue until the end of the file (EOF) is reached.
If the end of the file is reached we break out with a break command.

Before the program ends both files are closed using fclose() function.

Conclusion

Make sure you close the files and always use secure functions when possible.

That is all for this tutorial!

This entry was posted in C Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed. Tweet This! Tweet This! or use to share this post with others.

There are currently 2 responses to “C Tutorial – Copying a File”

Why not let us know what you think by adding your own comment!

  1. adlai on October 17th, 2013:

    can you please tell me, why there is an error on “errno_t”. is says unknown type name. thanks

  2. Dr P Bacon on November 3rd, 2013:

    int copy(char *from, char *to) {
    FILE *ffrom = fopen(from, “r”);
    if (!ffrom) return 0;
    FILE *fto = fopen(to, “w”);
    if (!fto) return 0;
    int inch;
    while (inch = fgetc(ffrom)) {
    if (inch == EOF) break;
    if (fputc(inch, fto) == EOF) break;
    }
    fclose(fto);
    fclose(ffrom);
    return 1;
    }