Read and Write Record From Binary File in C


#include "stdio.h"
#include "conio.h"


struct student
     {
      int rollno;
      char name[30];
      float per;
     }stud,*ptr,temp;


//Functions


int search(FILE *fp,char sname[30])
{
int record=1,no=1;
struct student stud;
fseek(fp,0L,SEEK_SET);
printf("\n Name : %s",sname);
record=1;
record=fread(&stud,sizeof(stud),1,fp);
while(no)
   {
    if(!strcmp(stud.name,sname))
      {
printf("\n Roll No    = %d",stud.rollno);
printf("\n Name       = %s",stud.name);
printf("\n Percentage = %f",stud.per);
fclose(fp);
return no;
      }
    record=fread(&stud,sizeof(stud),1,fp);
    no++;
    if(record!=1)
      {
      printf("\n Record Not Found...");
      fclose(fp);
      return 0;
      }
    }
}




void main()
{
int i,no,record,flag,pos=0,len;
float p;
FILE *fp;
char sname[30];
clrscr();


//To Write Into File
fp=fopen("D:/student.txt","wb");
printf("\n Enter How Many Student Data You Want to Enter : \n");
scanf("%d",&no);
for(i=0;no>i;i++)
   {
    printf("\n Enter Roll No = ");
    scanf("%d",&stud.rollno);
    printf("\n Enter Name = ");
    scanf("%s",stud.name);
    printf("\n Enter Percentage = ");
    scanf("%f",&p);
    stud.per=p;
    fwrite(&stud,sizeof(stud),1,fp);
   }
fclose(fp);


//To Read From File
fp=fopen("D:/student.txt","rb");
printf("\n Student Data Are as per Follow : \n");
for(i=0;no>i;i++)
   {
    fread(&stud,sizeof(stud),1,fp);
    printf("\n Roll No    = %d",stud.rollno);
    printf("\n Name       = %s",stud.name);
    printf("\n Percentage = %f",stud.per);
   }


//Go Back to BOF
printf("\n Enter Name To Search :");
scanf("%s",sname);
search(fp,sname);
fclose(fp);
getch();
clrscr();


fp=fopen("D:/student.txt","rb");
fseek(fp,0L,SEEK_SET);
printf("\n Enter Name To Replace Record :");
fflush(stdin);
scanf("%s",sname);
len=search(fp,sname);
if(!len)
{
printf("\n Record Not Found");
}
fclose(fp);


fp=fopen("D:/student.txt","r+b");
    fseek(fp,0L,SEEK_SET);
    printf("\n Enter Roll No = ");
    scanf("%d",&temp.rollno);
    printf("\n Enter Name = ");
    scanf("%s",temp.name);
    printf("\n Enter Percentage = ");
    scanf("%f",&p);
    temp.per=p;
    printf("\n Record = %d",--len);
    fseek(fp,sizeof(struct student)*len,SEEK_SET);
    fwrite(&temp,sizeof(struct student),1,fp);
fclose(fp);


getch();
clrscr();


fp=fopen("D:/student.txt","rb");
fseek(fp,0L,SEEK_SET);
printf("\n Student Data Are as per Follow : \n");
flag=1;
flag=fread(&stud,sizeof(stud),1,fp);


for(i=0;flag==1;i++)
   {
    printf("\n Roll No    = %d",stud.rollno);
    printf("\n Name       = %s",stud.name);
    printf("\n Percentage = %f",stud.per);
    flag=fread(&stud,sizeof(stud),1,fp);
   }


fclose(fp);
getch();
}

Enumerated Data Type Example


#include "stdio.h"
#include "conio.h"


enum days
{
sun=0,mon,tue,wed,thu,fri,sat
}day;


void main()
{
enum days start,end;
clrscr();
start=1;
end=6;
day=0;
day++;
printf("start = %d",start);
printf("\nEnd = %d",end);
switch(day)
{
case 0:
case 6:
   printf("\n Weekends");
   break;
case 1:
case 2:
case 3:
case 4:
case 5:
    printf("\n Working Day");
    break;
}


getch();
}

Find Position of Substring for Given String in C


#include "stdio.h"
#include "string.h"


int main(void)
{
   char *string1 = "abc defg hijk lmno pqr stu vw xyz";
   char *string2 = " ";
   char *ptr;


   ptr = strpbrk(string1, string2);


   if (ptr)
      printf("strpbrk found first character: %c\n", *ptr);
   else
      printf("strpbrk didn't find character in set\n");


   return 0;
}