Opening and Defining FILE in C Programming

January 27, 2010 No Comments » Hits : 278





Opening and Defining FILE in C Programming

  1. 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 :

  1. File name consists of two fields
  2. First field is name field and second field is of extension field
  3. Extension field is optional
Data Structure :
  1. Data structure of file is defined as FILE in the library of standered I/O functions
  2. In short we have to declare the pointer variable of type FILE

Mode of FILE opening :

  1. 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
}

Incoming search terms: