Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

WRITING TO AND READING FROM A FILE

Posted By: Harriet Hughes     Category: C Programming     Views: 15590

Write a program to read data from the keyboard, write it to a file called INPUT, again read the same data from the INPUT file, and display it on the screen.

Code for WRITING TO AND READING FROM A FILE in C Programming

   #include  <stdio.h>                                              
                                                                    
   main()                                                           
   { 
       FILE *f1;                                                    
       char c;                                                      
       printf("Data Input\n\n");
         /* Open the file INPUT */
f1 = fopen("INPUT", "w"); /* Get a character from keyboard */
while((c=getchar()) != EOF) /* Write a character to INPUT */
putc(c,f1); /* Close the file INPUT */
fclose(f1); printf("\nData Output\n\n"); /* Reopen the file INPUT */
f1 = fopen("INPUT","r"); /* Read a character from INPUT*/
while((c=getc(f1)) != EOF) /* Display a character on screen */
printf("%c",c); /* Close the file INPUT */
fclose(f1); } Output Data Input This is a program to test the file handling features on this system^Z Data Output This is a program to test the file handling features on this system
  
Share: 


Didn't find what you were looking for? Find more on WRITING TO AND READING FROM A FILE Or get search suggestion and latest updates.

Harriet Hughes
Harriet Hughes author of WRITING TO AND READING FROM A FILE is from London, United Kingdom.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!