Write-Read (using putc & getc) in C.

 

01  #include<stdio.h>
02
03 int main()
04 {
05 char c;
06
07 FILE *fp; // file pointer
08
09 fp=fopen("c:\my_text.txt","w"); //open a file for writing (file_path,mode)
10
11 while((c=getchar())!=EOF) //EOF denotes end of file (ctrl + Z) from keyboard
12 {
13 putc(c,fp); //writing a single character to a file
14 }
15 fclose(fp); //closing a file
16
17 printf("nReading data from file :n");
18
19 fp=fopen("c:\my_text.txt","r"); //open a file for reading (file_path,mode)
20
21 while((c=getc(fp))!=EOF) //EOF denotes end of file
22 {
23 putchar(c); //print character to screen
24 }
25 fclose(fp); // closing the file
26
27 return 0;
28 }
 
 OUTPUT :

Hey,
This is a file demo.^Z

Reading data from file :
Hey,
This is a file demo.

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *


The reCAPTCHA verification period has expired. Please reload the page.