create your own

File Copy Program in C Language

89
rate or flag this page

By rajkishor09


Today we are going to learn a simple file copy program in C language. As I said this is a simple file copy program so you should not expect its output like DOS copy command has. Ok let’s start.

The main logic behind this program is that we will open a file and copy its contents to new file character by character. So we are going to implement logic that we had used, especially as a student, in our school or college i.e. copying note from a friends’ notes.

For this purpose we will need two files, one source file and other will be new file. Source file will be opened in read- only mode because we are not going to modify any data of this file, we only need to read character. Newly created file will be opened in write mode because we need to write every data what we will find in source file.


Source Code

  1. #include<stdio.h>
  2. void main(int arg, char *arr[])
  3. {
  4. FILE *fs,*ft;
  5. char ch;
  6. if(arg!=3)
  7. {
  8. printf("Argument Missing ! Press key to exit.");
  9. getch();
  10. exit();
  11. }
  12. fs = fopen(arr[1],"r");
  13. if(fs==NULL)
  14. {
  15. printf("Cannot open source file ! Press key to exit.");
  16. getch();
  17. exit();
  18. }
  19. ft = fopen(arr[2],"w");
  20. if(ft==NULL)
  21. {
  22. printf("Cannot copy file ! Press key to exit.");
  23. fclose(fs);
  24. getch();
  25. exit();
  26. }
  27. while(1)
  28. {
  29. ch = getc(fs);
  30. if(ch==EOF)
  31. {
  32. break;
  33. }
  34. else
  35. putc(ch,ft);
  36. }
  37. printf("File copied succesfully!");
  38. fclose(fs);
  39. fclose(ft);
  40. }

Important Note

NOTE : Please don't save this program as "copy.c" otherwise it will not work. You can name it copyfile.c etc. I named it copyf.c.

Code Explanation

As you can see this program is not for those who has just began C programming in their school or college. This is for those who have idea of file handling, function, pointer array etc. So I’m not going to explain it line by line, only which I think is important will be explained.

Line – 2: This line is starting of the main function which accepts two arguments or parameters. First argument is an integer argument in the main function which stores the number of parameters provided at the runtime. In our program it has to be 3 (e.g. program_name source_file destination_file). Second argument in main function is a character array pointer, as you can see, which store the arguments.

Line – 4: This line declares two file pointer variables which we will need in our program. One file pointer variable will be used to read file and another will be used to write to file.

Line – 6-11: These line will check whether user has provided 3 arguments or not. If argument is less than 3 or greater than 3, then it will show error message.

Line – 12-18: In these lines we will open source file in read mode. To serve this purpose we will use “fopen()” function. This function requires two arguments, first is file name and second is file opening mode. It takes file name from “main()” function’s character array pointer argument. If “fopen()” function can open file successfully then returns pointer else returns NULL. We are checking this, if file is not found or file cannot be accessed due to security restriction then there is no need to execute program further.

Line – 19-26: In this block of code we are trying to create a new file. So we need to open this file in write mode. It also takes file name from “main()” function’s character array pointer argument. If fopen() function returns NULL then we need to close source file stream and exit program.

Line – 27-36: As you can see here we have declared an infinite while loop and the loop continues until end of file is reached. The “gets()” function reads character from given stream and stores character in ch variable then increments file pointer to next character. In line 35 we are writing character stored in ch variable to new file (ft file pointer) with the help of “putc()” function.

Line – 38-39: These two lines contain two same functions whose job is to clearing the stream buffer and closing the stream buffer. Because we are using two file pointer, so we need to close both of them.

I know this explanation is very concise for those who are new in file handling in C. But you can ask question about this topic through comment provided below. Thank you, keep visiting me.


Best of Amazon.com

Wii Fit Wii Fit
Price: $99.95
List Price: $89.99
Nintendo DSi Matte - Black Nintendo DSi Matte - Black
Price: $169.98
List Price: $169.99
PlayStation 3 Dualshock 3 Wireless Controller PlayStation 3 Dualshock 3 Wireless Controller
Price: $36.99
List Price: $54.99
Wii Charge Station Wii Charge Station
Price: $19.81
List Price: $29.99
Official Nintendo Wii Wheel Official Nintendo Wii Wheel
Price: $8.75
List Price: $14.99
Gold's Gym Cardio Workout Gold's Gym Cardio Workout
Price: $14.89
List Price: $19.99

Best books on C Language

C How to Program (5th Edition) C How to Program (5th Edition)
Price: $59.00
List Price: $123.00
C How to Program Introducing C++ and Java C How to Program Introducing C++ and Java
Price: $38.50
List Price: $115.00
Problem Solving and Program Design in C (6th Edition) Problem Solving and Program Design in C (6th Edition)
Price: $70.00
List Price: $105.00
C How to Program (3rd Edition) C How to Program (3rd Edition)
Price: $24.49
List Price: $85.00
C: How to Program (6th Edition) C: How to Program (6th Edition)
Price: $78.99
List Price: $123.00

Program Output

To run program properly you need to execute this program from DOS shell.
To run program properly you need to execute this program from DOS shell.
Successful completion of copy file.
Successful completion of copy file.

Comments

RSS for comments on this Hub

hAkUwoH123  says:
5 months ago

bubu ang sagot mo!!!!

James  says:
5 months ago

This was very helpful. Thank You. I am creating a function library for a low-level system and I do not need to use system calls, and this example needs a little modification on my part, but it helped put me on the right track. Thanks again.

Submit a Comment

Members and Guests

Sign in or sign up and post using a hubpages account.


optional


  • No HTML is allowed in comments, but URLs will be hyperlinked
  • Comments are not for promoting your hubs or other sites

working