FILE HANDLING OPERATIONS USING SWITCH CASE IN C.







Basics of File Handling in C



So far the operations using C program are done on a prompt / terminal which is not stored anywhere. But in the software industry, most of the programs are written to store the information fetched from the program. One such way is to store the fetched information in a file. Different operations that can be performed on a file are:
  1. Creation of a new file (fopen with attributes as “a” or “a+” or “w” or “w++”)
  2. Opening an existing file (fopen)
  3. Reading from file (fscanf or fgetc)
  4. Writing to a file (fprintf or fputs)
  5. Moving to a specific location in a file (fseek, rewind)
  6. Closing a file (fclose)
The text in the brackets denotes the functions used for performing those operations.
Functions in File Operations:




Opening or creating file
For opening a file, fopen function is used with the required access modes. Some of the commonly used file access modes are mentioned below.
File opening modes in C:
  • “r” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. If the file cannot be opened fopen( ) returns NULL.
  • “w” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file.
  • “a” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer that points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file.
  • “r+” – Searches file. If is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the first character in it. Returns NULL, if unable to open the file.
  • “w+” – Searches file. If the file exists, its contents are overwritten. If the file doesn’t exist a new file is created. Returns NULL, if unable to open file.
  • “a+” – Searches file. If the file is opened successfully fopen( ) loads it into memory and sets up a pointer which points to the last character in it. If the file doesn’t exist, a new file is created. Returns NULL, if unable to open file.
As given above, if you want to perform operations on a binary file, then you have to append ‘b’ at the last. For example, instead of “w”, you have to use “wb”, instead of “a+” you have to use “a+b”. For performing the operations on the file, a special pointer called File pointer is used which is declared as
FILE *filePointer; 
So, the file can be opened as 
filePointer = fopen(“fileName.txt”, “w”)
The second parameter can be changed to contain all the attributes listed in the above table.
  • Reading from a file –
    The file read operations can be performed using functions fscanf or fgets. Both the functions performed the same operations as that of printf and gets but with an additional parameter, the file pointer. So, it depends on you if you want to read the file line by line or character by character.
    And the code snippet for reading a file is as:
    FILE * filePointer; 
    filePointer = fopen(“fileName.txt”, “r”);
    fscanf(filePointer, "%s %s %s %d", str1, str2, str3, &year);
    
  • Writing a file –:
    The file write operations can be perfomed by the functions fprintf and fputs with similarities to read operations. The snippet for writing to a file is as :
    FILE *filePointer ; 
    filePointer = fopen(“fileName.txt”, “w”);
    fprintf(filePointer, "%s %s %s %d", "We", "are", "in", 2012);
    
  • Closing a file –:
    After every successful fie operations, you must always close a file. For closing a file, you have to use fclose function. The snippet for closing a file is given as .
  • #include<stdio.h>
    #include<conio.h>
    
    struct student
    {
    int rollno;
    char name[20];
    }s1;
    
    void insert()
    {
    FILE *fp;
    fp=fopen("files","a");
    printf("Enter rollno\n ");
    scanf("%d",&s1.rollno);
    printf("Enter name \n");
    scanf("%s",&s1.name);
    fwrite(&s1,sizeof(s1),1,fp);
    fclose(fp);
    }
    
    void display()
    {
    FILE *fp;
    fp=fopen("files","r");
    if(fp==NULL)
    printf("No such file\n");
    printf("\nRollno\tName\n");
    while(fread(&s1,sizeof(s1),1,fp))
    {
      printf("%d\t%s\n",s1.rollno,s1.name);
    }
    fclose(fp);
    }
    
    void search()
    {
     FILE *fp;
     int r,a=0;
     printf("\nEnter roll no to be searched\n");
     scanf("%d",&r);
     fp=fopen("files","r");
     if(fp==NULL)
     printf("No such file\n");
     while(fread(&s1,sizeof(s1),1,fp))
     {
        if(r==s1.rollno)
        {
        printf("Found\n");
        fwrite(&s1,sizeof(s1),1,fp);
        printf("%d %2s\n",s1.rollno,s1.name);
        a=1;
        }
     }
    fclose(fp);
    if(a==0)
    printf("\ndata not found\n");
    }
    
    void deleted()
    {
    FILE *fp;
    FILE *fp1;
    int r,a=0;
    printf("Enter roll no to delete data\n");
    scanf("%d",&r);
    fp=fopen("files","r");
    if(fp==NULL)
    printf("No such file\n");
    fp1=fopen("temp","w");
    while(fread(&s1,sizeof(s1),1,fp))
    {
       if(r!=s1.rollno)
       {
        fwrite(&s1,sizeof(s1),1,fp1);
        a=1;
       }
    
    }
    if(a==0)
    {printf("Record not found\n");}
    else
    {
    fclose(fp);
    fclose(fp1);
    fp=fopen("files","w");
    fp1=fopen("temp","r");
    while(fread(&s1,sizeof(s1),1,fp1))
    fwrite(&s1,sizeof(s1),1,fp);
    printf("Record deleted\n");
    fclose(fp);
    fclose(fp1);
    }
    }
    void update()
    {
    int r,a=0;
    FILE *fp;
    FILE *fp1;
    fp=fopen("files","r");
    fp1=fopen("temp","w");
    printf("Enter roll no to updated data\n");
    scanf("%d",&r);
     while(fread(&s1,sizeof(s1),1,fp))
        {
          if(r!=s1.rollno)
     {
       fwrite(&s1,sizeof(s1),1,fp1);
     }
        else
         {
     printf("Enter rollno\n");
     scanf("%d",&s1.rollno);
     printf("Enter name\n");
     scanf("%s",&s1.name);
     a=1;
     fwrite(&s1,sizeof(s1),1,fp1);
         }
        }
    
     fclose(fp1);
     fclose(fp);
      if(a==0)
      printf("Record not found\n");
      else
      {
     fp=fopen("files","w");
     fp1=fopen("temp","r");
    while(fread(&s1,sizeof(s1),1,fp1))
     fwrite(&s1,sizeof(s1),1,fp);
     fclose(fp);
     fclose(fp1);
     printf("Record updated\n");
    
    }
    }
    int menu()
    {
    int a;
    printf("\n*****Options*****");
    printf("\nEnter 1 to add data");
    printf("\nEnter 2 to search data");
    printf("\nEnter 3 to delete data");
    printf("\nEnter 4 to modify data");
    printf("\nEnter 5 for display data");
    printf("\nEnter 6 to exit");
    printf("\nEnter your choice\n");
    scanf("%d",&a);
    return a;
    }
    void main()
    {
    int a=0;
    clrscr();
    do
    {
    switch(a=menu())
    {
      case 1:
       insert();
       break;
      case 2:
       search();
       break;
      case 3:
       deleted();
       break;
      case 4:
       update();
       break;
      case 5:
       display();
       break;
      case 6:
      printf("*****Thankyou*****");
      break;
    }
    }while(a!=6);
    
    getch();
    }

No comments:

Post a Comment

If you have any problems related to solutions or any concept please let me know.

Copyright (c) 2020 Custom Programs All Right Reserved

Pages