Program to write and read a file in c language

How to write a program to write and read a file in c language   

In this post we learning about How to write a program to write and read a file in c language.

What is file?

File is more flexible approach than the previous data entry approach. Files are the mega data structure in information processing. Storage of information and its retrieval are the vital system design and information system. By using files data can be stored on the disk where a group of related data (records and fields) is stored permanently. By using the disks and can be read whenever you want without destroying the data. A file is a place on files we can sense the data. Files establish a permanent links between inputs and outputs as data can be stored and retrieved. A computer programmer or data entry operator always prefer to enter data in the files instead of storing the data temporary in the main memory using the direct keyboard facility. 

program to write and read a file in c language

Reading and writing to a text file

In c language for reading and writing to a text file, we use the functions fprintf() and fscanf().

They are just the file versions of printf() and scanf(). The only difference is that fprintf() and fscanf() expects a pointer to the structure FILE.

Program to write and read a file in c language   

#include<stdio.h>

#include<conio.h> 

main()

{

FILE *fl;

char c;

clrscr();

printf("\nDATA INPUT:\n\n");

fl-fopen("input","w"); 

while((c=getchar())!=EOF) 

{

putc(c,fl); 

}

fclose(fl); 

printf("\nDATA OUTPUT:\n\n"); 

fl=fopen("input","r"); 

while((c=getc(fl))!=EOF) 

printf("%c",c); fclose(fl); 

}

Output:

DATA INPUT: 

Sham is a good boy. 

DATA OUTPUT 

Sham is a good boy. 

Previous
Next Post »

Please do not entering spam link in the comment box ConversionConversion EmoticonEmoticon