Opening and Defining FILE in C Programming
- Before storing data onto the secondary storage , firstly we must specify following things -
- File name
- Data Structure
- Perpose / Mode
- Very first task in File handling is to open file
File name :
- File name consists of two fields
- First field is name field and second field is of extension field
- Extension field is optional
Data Structure :
- Data structure of file is defined as FILE in the library of standered I/O functions
- In short we have to declare the pointer variable of type FILE
Mode of FILE opening :
- Depending upon the operation , select the mode eg. Reading,Writing,appending etc
Syntax of Declaring FILE pointer
FILE *fp;
Syntax of Opening file hello.txt
fp = fopen ("filename","mode");Live Example :
#include<stdio.h> void main() { FILE *fp; char ch; fp = fopen("INPUT.txt","r") // Open file in Read mode while(1) { ch = fgetc(fp); // Read a Character if(ch == EOF ) // Check for End of File break ; printf("%c",ch); } fclose(fp); // Close File after Reading }


