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;
}

Quick Sort in C


#include "stdio.h"
#include "conio.h"
void Quick_Sort(int *,int ,int );
int p=0;
void main()
{
int k[10]={42,23,74,11,65,58,94,36,99,87};
int lb=0,ub=9,i;
clrscr();
Quick_Sort(k,lb,ub);
for(i=0;i<10;i++)
   {
   //printf(" %d ",k[i]);
   }
getch();
}
void Quick_Sort(int k[10],int lb,int ub)
{
int flag=1;
int key,temp,i,j,m;
if(ub>lb)
  {
   i=lb;
   j=ub+1;
   key = k[lb];
   while(flag)
{
i=i+1;
while(k[i]
     {
     i=i+1;
     }
j=j-1;
while(k[j]>key)
     {
     j=j-1;
     }
if(i
  {
  printf("\n Interchange : %d <-> %d \n",k[i],k[j]);
  temp=k[i];
  k[i]=k[j];
  k[j]=temp;
  }
else
  {
   flag=0;
  }
}
     printf("\n By First :  %d <-> %d \n",k[lb],k[j]);
     temp=k[lb];
     k[lb]=k[j];
     k[j]=temp;
     printf("\n pass = %d \n",p++);
     for(m=0;m<10;m++)
{
printf(" %d ",k[m]);
}
     Quick_Sort(k,lb,j-1);
     Quick_Sort(k,j+1,ub);
  }
  else
  {
   return;
  }
}

GTU Network Programming Practical List


GTU Network Programming Practical List

1.   Write a program of VRC.
2.   Write a program of LRC.
3.   Write a program of CRC.
4.   Write a program of Check Sum.
5.   Write a program of one bit error detection and correction method.
          (Hamming Distance Method)
6.   Write a program for Byte stuffing. (For multiple frames)
7.   Write a program for Bit stuffing. (For multiple frames)
8.   Write program to implement data link layer protocol 1.
9.   Implement the protocol 2 for the following criteria.
          Frame size should be 100 to 300.
10.  Implement the protocol 3 for the following criteria.
       Frame size should be 100 to 300
       Framing techniques is byte stuffing.
       Send the Positive Ack for frame 1 to 4.
       Send the Negative Ack for frame 5.
       Send the frame 8 again because of time out.
11.  Implement the protocol 4  for the following criteria.
       Frame size should be 100 to 300.
       Framing techniques is bit stuffing.
       Send the Positive Ack for frame 1 to 4.
       Send the Negative Ack for frame 5.
       Send the frame 8 again because of time out.
12.  Implement the protocol 4 for the following criteria.
       Frame size should be 100 to 300
       Framing techniques is either byte stuffing or bit
       stuffing
       Send the Positive Ack for frame 1 to 4.
       Send the Negative Ack for frame 5.
       Send the frame 8 again because of time out.
13.  Implement the protocol 5 for the following criteria.
       Sliding window size is 5.
       Frame size is 100.
       Send the Positive Ack for frame 1 to 4.
       Send the Negative Ack for frame 5.
       Send the frame 8 again because of time out.
14.  Implement the protocol 6 for the following criteria.
       Sliding window size is 5.
       Frame size is 100.
       Send the Positive Ack for frame 1 to 4.
       Send the Negative Ack for frame 5.
       Send the frame 8 again because of time out.
15.  Write a program of shortest path.
16.  Write a program of Caesar Cipher.
17.  Write a program of Substitution Cipher. (S - Box)
18.  Write a program of Transposition Cipher. (P - BOX)
19.  Write a program of product Cipher.  (Combine S & P box)
20.  Write a program of RSA.
21.  Write a program of different Cipher Modes.

Write a program of Vertical Redundancy Check - VRC

Header File
========
void bitptrn(char ch)
{
        int chsize,bitset;
        for(chsize=(sizeof(char)*8)-1;chsize>=0;chsize--)
        {
                bitset=1<
                if(ch & bitset)
                {
                        printf("1");
                }
                else
                {
                        printf("0");
                }
        }
}

int noof1bit(char ch)
{
        int chsize,bitset,pbit;
        for(chsize=(sizeof(char)*8)-1,pbit=0;chsize>=0;chsize--)
        {
                bitset=1 << chsize;
                if(ch & bitset)
                {
                        pbit++;
                }
        }
        return(pbit);
}

Sender Program
============
int main()
{
        char ipch;
        int pbit,pout;
        FILE *infile;
        system("clear");
        infile=fopen("data.txt","r");
        pout=open("vrcpipe","O_WRONLY");
        printf("-----------------------------------\n");
        printf("Character   BitPattern   NewPattern");
        printf("\n-----------------------------------");
        while(feof(infile)==0)
        {
                ipch=getc(infile);
                if(feof(infile)==0)
                {
                        printf("\n    %c        ",ipch);
                        bitptrn(ipch);
                        pbit=noof1bit(ipch);
                        if((pbit%2)==1)
                                pbit=1<<7;
                        else
                                pbit=0;
                        ipch=ipch|pbit;
                        printf("     ");
                        bitptrn(ipch);
                        write(pout,&ipch,sizeof(char)); //write to pipe
                }//if over
        }//while over
        fclose(infile);
        printf("\n-----------------------------------");
        printf("\n\nPress enter key to exit...\n");
        system("read x");
        system("clear");
        return(0);
}

Receiver Program
============
int main()
{
        char ipch;
        int pbit,pin;
        const int bitset=127;
FILE *pin;
        system("clear");
        pin=fopen("vrcpipe","r");
        printf("-----------------------------------\n");
        printf("Character   BitPattern   OldPattern\n");
        printf("-----------------------------------");
while()
        {
                printf("\n    %c        ",ipch);
                bitptrn(ipch);
                pbit=noof1bit(ipch);
                if((pbit%2)==1)
                {
                        printf("\n\nError : Data received is incorrect");
                        exit(0);
                }
                ipch=ipch & bitset;
                printf("     ");
                bitptrn(ipch);
        }
        printf("\n-----------------------------------");
        printf("\n\nPress enter key to exit...\n");
        system("read x");
        system("clear");
        return(0);
}