Monday, December 18, 2017

c Program in file input & output

File Handling in c language is used to open, read, write, search or close file. It is used for permanent storage.


Advantage of File
It will contain the data even after program exit. Normally we use variable or array to store data, but data is lost after program exit. Variables and arrays are non-permanent storage medium whereas file is permanent storage medium.


Functions for file handling
There are many functions in C library to open, read, write, search and close file. A list of file functions are given below:
No.
Function
Description
1
fopen()
opens new or existing file
2
fprintf()
write data into file
3
fscanf()
reads data from file
4
fputc()
writes a character into file
5
fgetc()
reads a character from file
6
fclose()
closes the file
7
fseek()
sets the file pointer to given position
8
fputw()
writes an integer to file
9
fgetw()
reads an integer from file
10
ftell()
returns current position
11
rewind()
sets the file pointer to the beginning of the file
ModeDescription
ropens a text file in read mode
wopens a text file in write mode
aopens a text file in append mode
r+opens a text file in read and write mode
w+opens a text file in read and write mode
a+opens a text file in read and write mode
rbopens a binary file in read mode
wbopens a binary file in write mode
abopens a binary file in append mode
rb+opens a binary file in read and write mode
wb+opens a binary file in read and write mode
ab+opens a binary file in read and write mode
  1. #include <stdio.h>  
  2. main(){  
  3.    FILE *fp;  
  4.    fp = fopen("file.txt""w");//opening file  
  5.    fprintf(fp, "Hello file by fprintf...\n");//writing data into file  
  6.    fclose(fp);//closing file  
  7. }  

No comments:

Post a Comment