Thursday, June 14, 2012

File operation in ‘C’



File operation in any programming language is meant for saving the data, so it can be called as data file. C is still is a low-level language if we goes by the present standard. In C file operations mainly depends on streams of bytes, that are known as "input streams" or "output streams". C language does not support any Random files as some of previous languages like COBOL and BASIC. All required function for File Operations are mentioned in "stdio.h", it was written by Mike Lesk at Bell Labs in the early 1970s.

Main functions for File Operations -

FILE pointer from "stdio.h" must be use to perform File operations.

Example :
FILE *fp;

There are many input/output statement that mainly meant for file operations. If we opened a file then we should close.

fopen(), function used to open a file. We can open a file for reading or writing or for modifying purpose too.

"r"- Read-Only mode.
"w"-Write-only mode.
"a"-append mode.
"r+"-read+write mode.
"w+"-write+read mode.
"a+"-read+append mode.

fclose() - is for closing a file.

For reading, writing or searching a record we can using following functions
fgetc()
fgets()
fscanf()
fputc()
fprintf()
fputs()
fseek()
rewind()

Here is a few example on File Operation, where we will try to cover a few functions that exclusively related to the file operation.


//Writing and Reading character wise using putc() and getc()
#include <stdio.h>
#include <conio.h>
#include <string.h>

  void FileWrite()
      {
           char Filename[11],sentence[100];
           int i=0;
           FILE *fptr;
           fflush(stdin);
           printf("\n\tEnter Filename   : ");
           gets(Filename);
           fflush(stdin);
           printf("\n\tEnter a Sentence :  ");
           gets(sentence);
           fptr=fopen(Filename,"w");
           do
              {
                   putc(sentence[i++], fptr);
              }while(sentence[i]!='\0');
           fclose(fptr);
      }

  void FileRead()
      {
           char Filename[11],sentence[100];
           int i=0;
           FILE *fptr;
           fflush(stdin);
           printf("\n\tEnter Filename   : ");
           gets(Filename);
           if ((fptr = fopen(Filename, "r"))== NULL)
              {
                   printf("\n\n\tFile Doesnot Exists ");
                   getch();
                   return;
              }
           fptr=fopen(Filename,"r");
           while((sentence[i++]=getc(fptr))!=EOF);
           sentence[i]='\0';
           fclose(fptr);
           printf("\n\tSentence Extract from File is = %s ",sentence);
      }

  void main()
      {
           int c=0;
           clrscr();
           while(c<3)
              {
                   printf("\n\n\n\t1. Write\n\t2. Read\n\t3. Exit");
                   printf("\n\tenter your choice:");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1:FileWrite();break;
                            case 2:FileRead();break;
                        }
              }
           getch();
      }




//Writing and Reading whole string using fputs() and fgets()
#include <stdio.h>
#include <conio.h>
#include <string.h>

  FILE *fp;
  int c;
  void FileCreate()
      {
           char wrd[50],ch='y';
           int L;
           fp=fopen("Word.txt","w");
           while(ch=='y' || ch=='Y')
              {
                   fflush(stdin);
                   printf("\n\tEnter a String : ");
                   gets(wrd);
                   fputs(wrd,fp);
                   fputs("\n",fp);
                   fflush(stdin);
                   printf("\n\tWant to continue y/n? ");
                   ch=getche();
              }
           fclose(fp);
      }
  void FileRead()
      {
           char wrd[50];
           if((fp=fopen("word.txt","r"))==NULL)
              {
                   printf("\n\n\tInvalid Filename");
                   getch();
                   return;
              }
           printf("\n\tStrings read from the file :");
           while(fgets(wrd,50,fp)!=NULL)
              {
                   printf("\n\t%s",wrd);
              }
           fclose(fp);
      }
  void main()
      {
           int c=0;
           clrscr();
           while(c<3)
              {
                   printf("\n\n\n\t1. Write\n\t2. Read\n\t3. Exit");
                   printf("\n\tenter your choice:");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1:FileCreate();break;
                            case 2:FileRead();break;
                        }
              }
           getch();
      }


//Writing and Reading using fprintf() and fscanf()
#include <stdio.h>
#include <conio.h>
#include <string.h>

  void FileCreate()
      {
           char ch='y',name[20];
           int age,i;
           FILE *fp;
           fp=fopen("Details.txt","w");
           while(ch=='y' || ch=='Y')
              {
                   printf("\n\tEnter name : ");
                   scanf("%s",&name);
                   printf("\n\tEnter age : ");
                   scanf("%d",&age);
                   fprintf(fp,"%s %d",name,age);
                   printf("\n\tWant to continue (y/n)? ");
                   ch=getche();
                   printf("\n");
              }
           fclose(fp);
      }

  void FileRead()
      {
           char name[20];
           int age,i;
           FILE *fp;
           fp=fopen("Details.txt","r");
           while((fscanf(fp,"%s %d",&name,&age))!=EOF)
              {
                   printf("\n\n\tname : %s ",name);
                   printf("\n\tAge :  %d",age);
              }
           fclose(fp);
           getch();
      }

  void main()
      {
           int c=0;
           clrscr();
           while(c<3)
              {
                   printf("\n\n\n\t1. Write\n\t2. Read\n\t3. Exit");
                   printf("\n\tenter your choice:");
                   scanf("%d",&c);
                   switch(c)
                        {
                            case 1:FileCreate();break;
                            case 2:FileRead();break;
                        }
              }
           getch();
      }




No comments: