Write a C program which copies one file to another.
(Note: The file name and n are specified on the command line.)
Algorithm:
Step 1: Start Step 2: read command line arguments Step 3: check if no of arguments = 3 or not. If not print invalid no of arguments Step 4: open source file in read mode Step 5: if NULL pointer, then print source file can not be open Step 6: open destination file in write mode Step 7: if NULL pointer, then print destination file can not be open Step 8 : read a character from source file and write to destination file until EOF Step 9: close source file and destination file Step 10: Stop
Program:
#include <stdio.h> #include <conio.h> void main() { FILE *f1, *f2; char c; clrscr(); printf(“Enter the data to the file1.txt file \n”); f1 = fopen(“file1.txt”, “w”); while((c = getchar()) != EOF) putc(c, f1); fclose(f1); f2 = fopen(“file2.txt”, “w”); f1 = fopen(“file1.txt”, “r”); while((c = getc(f1)) != EOF) putc(c,f2); fclose(f1); fclose(f2); printf(“after copying the data in file2.txt file is \n”); f2 = fopen(“file2.txt”, “r”); while((c = getc(f2)) != EOF) printf(“%c”, c); fclose(f2); getch(); }
Input & Output:
Enter the data to the file1.txt file STUDENT BOX OFFICE.IN After copying the data in file2.txt file is STUDENT BOX OFFICE.IN
-
UpdatedOct 21, 2014
-
Views19,949
You May Like
Write a C program to find the roots of a quadratic equation
.
Write a C program to count the lines, words and characters in a given text.
Write a C program to find the sum of individual digits of a positive integer.
Write a C program, which takes two integer operands and one operator from the user, performs the operation and then prints the result. (Consider the operators +,-,*, /, % and use switch statement
)
Write a C program to generate all the prime numbers
between 1 and n, where n is a value supplied by the user.
Write C programs that use both recursive
and non-recursive functions
- To find the
factorial
of a given integer. - To find the
GCD
(greatest common divisor) of two given integers.