Write a C program to display the contents of a file.
Algorithm:
Step 1: Start Step 2: open a empty file in write mode. Step 3: if it is not end of file Step 4: write data into that file. Step 5: close the write mode operation Step 6: now open that file in read mode. Step 7: the contents of the file will be displayed Step 8: Stop
Program:
#include<stdio.h> #include<conio.h> FILE *fp1,*fp2; char c; void main() { clrscr(); printf("enter the text\n"); fp1 = fopen("abc.txt", "w"); while((c = getchar()) != EOF) putc(c, fp1); fclose(fp1); fp1 = fopen("abc.txt","r"); fp2=fopen("xyz.txt","w"); while(!feof(fp1)) { c = getc(fp1); putc(c,fp2); } fclose(fp1); fclose(fp2); printf("the copied data is \n"); fp2 = fopen("xyz.txt", "r"); while(!feof(fp2)) { c = getc(fp2); printf("%c", c); } getch(); }
Input & Output:
enter the text
engineering students are very good.
^Z
the copied data is
engineering students are very good.
-
UpdatedOct 21, 2014
-
Views25,881
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.