-2
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
#include<graphics.h>
int citem=0,ctemp=0;


int count;
void menu();


struct item{
int icode;
char iname[30];
int quantity;
float price;
};

void modify();
struct item i;
void additem();
void display(struct item );
void delete_item();
void main()
{
char ch,another;
int num;
FILE *fp,*ft;

int code;
long int recsize;
fp= fopen("d:\\kushal\\item.dat","rb+");
if(fp==NULL)
{
fp=fopen("d:\\kushal\\item.dat","wb+");
if(fp==NULL)
    {
        printf("\nFile can not be opened");
        getch();
        exit(1);
            }
            }
    recsize=sizeof(i);
         while(1)
         {
         clrscr();
        menu();
        display(i);
        gotoxy(0,47);
        printf("\n-------------------------------------------------------------------------------");
        gotoxy(3,48);
        textcolor(7);
        cprintf("A_Add");

        gotoxy(13,48);
        textcolor(7);
        cprintf("D_Display");

        gotoxy(30,48);
        textcolor(7);
        cprintf("M_Modify");

        gotoxy(50,48);
        textcolor(7);
        cprintf("X_Delete");

        gotoxy(65,48);
        cprintf("ESC_Exit");

        gotoxy(56,48);
           //   printf("Enter your choice:");

        fflush(stdin);
        ch=getche();

        switch(ch)
        {
        case 'a':           additem();
                  /*    fseek(fp,0,SEEK_END);
                another='y';
                while((another=='Y') || (another=='y'))
            {

                clrscr();
                printf("\t\tADD ITEM\n");
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fwrite(&i,recsize,1,fp);
                rewind(fp);
                 // menu();
                 //while(fread(&i,recsize,1,fp)==1)

                 printf("\n Do u want to Add more (y/n): ");
                 fflush(stdin);
                 another=getche();
                }
                getch();
                      */
                break;
         case 'd' :
                 rewind(fp);
                 while(fread(&i,recsize,1,fp)==1)
                 printf("\n%d\t%s\t%d\t%f",i.icode,i.iname,i.quantity,i.price);
                 getch();
                 break;


         case 'm' :            modify();
                  /*
                 another='y';
                while((another=='Y') || (another=='y'))
                {
                    clrscr();
                    printf("\t\tMODIFY\n\n");
                printf("\nItem code to modify: ");
                scanf("%d",&code);
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1)
                {
                if(code==i.icode)
                {
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fseek(fp,-recsize,SEEK_CUR);
                fwrite(&i,recsize,1,fp);
                break;
                    }
                      //    else{
                    //printf("\n Invalid Item code");
                       //   }
                      }
                      rewind(fp);
                 printf("\nSuccessfully Modified");
                 printf("\nDo u want to modify more record (y/n)");
                 fflush(stdin);
                 another=getche();

                     }
                     */
                    break;
         case 'x' :     delete_item();
                 /* another='y';
                  int n;
                while((another=='Y') || (another=='y'))
                {
                clrscr();
                printf("\t\tDELETE\n");
                printf("\n Enter the item code to delete: ");
                scanf("%d",&code);
                ft=fopen("d:\\kushal\\temp.dat","wb");
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1)
                if(code!=i.icode){
                fwrite(&i,recsize,1,ft);
                ctemp++;
                }
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1){
                citem++;
                }
                if(ctemp==citem){
                printf("\nInvalid Item code\n");
                }

                fclose(fp);
                fclose(ft);
                remove("d:\\kushal\\item.dat");
                rename("d:\\kushal\\temp.dat","d:\\kushal\\item.dat");
                fopen("d:\\kushal\\item.dat","rb+");
                printf("\nDo you want to continue(y/n)\n");
                fflush(stdin);
                another=getche();


                }
                */
                break;

         case 27 :      exit(0);

                 }
                }
             }
void menu(){
      /*    char choice;
while(1){
        gotoxy(3,2);
        textbackground(4);
        textcolor(7);
        printf("S.NO");
        gotoxy(10,2);
        printf("ITEM CODE");
        gotoxy(30,2);
        printf("ITEM NAME");
        gotoxy(50,2);
        printf("QUANTITY");
        gotoxy(65,2);
        printf("PRICE");

        fflush(stdin);

        choice=getch();
        if(choice==0)
        {
        fflush(stdin);
        choice=getch();
        }

           switch(choice){

            case 27:
                    exit(1);
                    break;

            case 72:
                       //   printf("\nup\n");
                    break;

            case 80:
                       //   printf("down");
                    break;
                    }
                    }
                    }*/

           clrscr();
           printf("===============================================================================");
        gotoxy(3,2);
        textbackground(1);
        textcolor(7);
        cprintf("S.NO");
        gotoxy(10,2);
        cprintf("ITEM CODE");
        gotoxy(30,2);
        cprintf("ITEM NAME");
        gotoxy(50,2);
        cprintf("QUANTITY");
        gotoxy(65,2);
        cprintf("PRICE");
        textcolor(7);

        printf("\n===============================================================================\n");
        }


 void display(struct item i){
        FILE *fp;
        int j=4;
        count=1;
        fp=fopen("d:\\kushal\\item.dat","rb");
         rewind(fp);
         //clrscr();
         while(fread(&i,sizeof(i),1,fp)==1){
         gotoxy(4,j);
         printf("%d",count);
         gotoxy(10,j);
         printf("%d\t ",i.icode);
        gotoxy(30,j);
        printf(" %s\t",i.iname);
        gotoxy(50,j);
        printf(" %d\t",i.quantity);
        gotoxy(65,j);
        printf("%.1f",i.price);
        j++;
        count++;
        }
        }

void additem(){
        char another;
        FILE *fp;

        int recsize=sizeof(i);
        fseek(fp,0,SEEK_END);
                another='y';
                while((another=='Y') || (another=='y'))
            {

                clrscr();
                printf("\t\tADD ITEM\n");
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fwrite(&i,recsize,1,fp);
                rewind(fp);
                 // menu();
                 //while(fread(&i,recsize,1,fp)==1)

                 printf("\n Do u want to Add more (y/n): ");
                 fflush(stdin);
                 another=getche();
                }
                getch();
}
void delete_item(){
    int code;
    FILE *fp,*ft;
    int recsize=sizeof(i);
          char another='y';
                  int n;
                while((another=='Y') || (another=='y'))
                {
                clrscr();
                printf("\t\tDELETE\n");
                printf("\n Enter the item code to delete: ");
                scanf("%d",&code);
                ft=fopen("d:\\kushal\\temp.dat","wb");
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1)
                if(code!=i.icode){
                fwrite(&i,recsize,1,ft);
                ctemp++;
                }
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1){
                citem++;
                }
                if(ctemp==citem){
                printf("\nInvalid Item code\n");
                }

                fclose(fp);
                fclose(ft);
                remove("d:\\kushal\\item.dat");
                rename("d:\\kushal\\temp.dat","d:\\kushal\\item.dat");
                fopen("d:\\kushal\\item.dat","rb+");
                printf("\nDo you want to continue(y/n)\n");
                fflush(stdin);
                another=getche();

                  }
                  }

                  void modify(){
                  int code,recsize=sizeof(i);
                  FILE *fp;
                  char another='y';
                while((another=='Y') || (another=='y'))
                {
                    clrscr();
                    printf("\t\tMODIFY\n\n");
                printf("\nItem code to modify: ");
                scanf("%d",&code);
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1)
                {
                if(code==i.icode)
                {
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fseek(fp,-recsize,SEEK_CUR);
                fwrite(&i,recsize,1,fp);
                break;
                    }
                      //    else{
                    //printf("\n Invalid Item code");
                       //   }
                      }
                      rewind(fp);
                 printf("\nSuccessfully Modified");
                 printf("\nDo u want to modify more record (y/n)");
                 fflush(stdin);
                 another=getche();

}                    }

Edited by Reverend Jim: Fixed formatting

2
Contributors
1
Reply
3
Views
6 Years
Discussion Span
Last Post by java_programmer
0
  1. When posting some code, use the Code tool so that it is readable.
  2. Specify the problem you facing. Just only pasting the code is not suggestable.
  3. If possible, post the error message you getting when compiling/running the program.

    include<stdio.h>

    include<conio.h>

    include<stdlib.h>

    include<string.h>

    include<graphics.h>

    int citem=0,ctemp=0;

    int count;
    void menu();

    struct item{
    int icode;
    char iname[30];
    int quantity;
    float price;
    };

    void modify();
    struct item i;
    void additem();
    void display(struct item );
    void delete_item();
    void main()
    {
    char ch,another;
    int num;
    FILE fp,ft;

    int code;
    long int recsize;
    fp= fopen("d:\kushal\item.dat","rb+");
    if(fp==NULL)
    {
    fp=fopen("d:\kushal\item.dat","wb+");
    if(fp==NULL)
    {
    printf("\nFile can not be opened");
    getch();
    exit(1);
    }
    }
    recsize=sizeof(i);
    while(1)
    {
    clrscr();
    menu();
    display(i);
    gotoxy(0,47);
    printf("\n-------------------------------------------------------------------------------");
    gotoxy(3,48);
    textcolor(7);
    cprintf("A_Add");

        gotoxy(13,48);
        textcolor(7);
        cprintf("D_Display");
    
        gotoxy(30,48);
        textcolor(7);
        cprintf("M_Modify");
    
        gotoxy(50,48);
        textcolor(7);
        cprintf("X_Delete");
    
        gotoxy(65,48);
        cprintf("ESC_Exit");
    
        gotoxy(56,48);
           //   printf("Enter your choice:");
    
        fflush(stdin);
        ch=getche();
    
        switch(ch)
        {
        case 'a':           additem();
                  /*    fseek(fp,0,SEEK_END);
                another='y';
                while((another=='Y') || (another=='y'))
            {
    
                clrscr();
                printf("\t\tADD ITEM\n");
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fwrite(&i,recsize,1,fp);
                rewind(fp);
                 // menu();
                 //while(fread(&i,recsize,1,fp)==1)
    
                 printf("\n Do u want to Add more (y/n): ");
                 fflush(stdin);
                 another=getche();
                }
                getch();
                      */
                break;
         case 'd' :
                 rewind(fp);
                 while(fread(&i,recsize,1,fp)==1)
                 printf("\n%d\t%s\t%d\t%f",i.icode,i.iname,i.quantity,i.price);
                 getch();
                 break;
    
    
         case 'm' :            modify();
                  /*
                 another='y';
                while((another=='Y') || (another=='y'))
                {
                    clrscr();
                    printf("\t\tMODIFY\n\n");
                printf("\nItem code to modify: ");
                scanf("%d",&code);
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1)
                {
                if(code==i.icode)
                {
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fseek(fp,-recsize,SEEK_CUR);
                fwrite(&i,recsize,1,fp);
                break;
                    }
                      //    else{
                    //printf("\n Invalid Item code");
                       //   }
                      }
                      rewind(fp);
                 printf("\nSuccessfully Modified");
                 printf("\nDo u want to modify more record (y/n)");
                 fflush(stdin);
                 another=getche();
    
                     }
                     */
                    break;
         case 'x' :     delete_item();
                 /* another='y';
                  int n;
                while((another=='Y') || (another=='y'))
                {
                clrscr();
                printf("\t\tDELETE\n");
                printf("\n Enter the item code to delete: ");
                scanf("%d",&code);
                ft=fopen("d:\\kushal\\temp.dat","wb");
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1)
                if(code!=i.icode){
                fwrite(&i,recsize,1,ft);
                ctemp++;
                }
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1){
                citem++;
                }
                if(ctemp==citem){
                printf("\nInvalid Item code\n");
                }
    
                fclose(fp);
                fclose(ft);
                remove("d:\\kushal\\item.dat");
                rename("d:\\kushal\\temp.dat","d:\\kushal\\item.dat");
                fopen("d:\\kushal\\item.dat","rb+");
                printf("\nDo you want to continue(y/n)\n");
                fflush(stdin);
                another=getche();
    
    
                }
                */
                break;
    
         case 27 :      exit(0);
    
                 }
                }
             }
    

    void menu(){
    /* char choice;
    while(1){
    gotoxy(3,2);
    textbackground(4);
    textcolor(7);
    printf("S.NO");
    gotoxy(10,2);
    printf("ITEM CODE");
    gotoxy(30,2);
    printf("ITEM NAME");
    gotoxy(50,2);
    printf("QUANTITY");
    gotoxy(65,2);
    printf("PRICE");

        fflush(stdin);
    
        choice=getch();
        if(choice==0)
        {
        fflush(stdin);
        choice=getch();
        }
    
           switch(choice){
    
            case 27:
                    exit(1);
                    break;
    
            case 72:
                       //   printf("\nup\n");
                    break;
    
            case 80:
                       //   printf("down");
                    break;
                    }
                    }
                    }*/
    
           clrscr();
           printf("===============================================================================");
        gotoxy(3,2);
        textbackground(1);
        textcolor(7);
        cprintf("S.NO");
        gotoxy(10,2);
        cprintf("ITEM CODE");
        gotoxy(30,2);
        cprintf("ITEM NAME");
        gotoxy(50,2);
        cprintf("QUANTITY");
        gotoxy(65,2);
        cprintf("PRICE");
        textcolor(7);
    
        printf("\n===============================================================================\n");
        }
    

    void display(struct item i){
    FILE *fp;
    int j=4;
    count=1;
    fp=fopen("d:\kushal\item.dat","rb");
    rewind(fp);
    //clrscr();
    while(fread(&i,sizeof(i),1,fp)==1){
    gotoxy(4,j);
    printf("%d",count);
    gotoxy(10,j);
    printf("%d\t ",i.icode);
    gotoxy(30,j);
    printf(" %s\t",i.iname);
    gotoxy(50,j);
    printf(" %d\t",i.quantity);
    gotoxy(65,j);
    printf("%.1f",i.price);
    j++;
    count++;
    }
    }

    void additem(){
    char another;
    FILE *fp;

        int recsize=sizeof(i);
        fseek(fp,0,SEEK_END);
                another='y';
                while((another=='Y') || (another=='y'))
            {
    
                clrscr();
                printf("\t\tADD ITEM\n");
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fwrite(&i,recsize,1,fp);
                rewind(fp);
                 // menu();
                 //while(fread(&i,recsize,1,fp)==1)
    
                 printf("\n Do u want to Add more (y/n): ");
                 fflush(stdin);
                 another=getche();
                }
                getch();
    

    }
    void delete_item(){
    int code;
    FILE fp,ft;
    int recsize=sizeof(i);
    char another='y';
    int n;
    while((another=='Y') || (another=='y'))
    {
    clrscr();
    printf("\t\tDELETE\n");
    printf("\n Enter the item code to delete: ");
    scanf("%d",&code);
    ft=fopen("d:\kushal\temp.dat","wb");
    rewind(fp);
    while(fread(&i,recsize,1,fp)==1)
    if(code!=i.icode){
    fwrite(&i,recsize,1,ft);
    ctemp++;
    }
    rewind(fp);
    while(fread(&i,recsize,1,fp)==1){
    citem++;
    }
    if(ctemp==citem){
    printf("\nInvalid Item code\n");
    }

                fclose(fp);
                fclose(ft);
                remove("d:\\kushal\\item.dat");
                rename("d:\\kushal\\temp.dat","d:\\kushal\\item.dat");
                fopen("d:\\kushal\\item.dat","rb+");
                printf("\nDo you want to continue(y/n)\n");
                fflush(stdin);
                another=getche();
    
                  }
                  }
    
                  void modify(){
                  int code,recsize=sizeof(i);
                  FILE *fp;
                  char another='y';
                while((another=='Y') || (another=='y'))
                {
                    clrscr();
                    printf("\t\tMODIFY\n\n");
                printf("\nItem code to modify: ");
                scanf("%d",&code);
                rewind(fp);
                while(fread(&i,recsize,1,fp)==1)
                {
                if(code==i.icode)
                {
                printf("\nItem code: ");
                scanf("%d",&i.icode);
                printf("\nItem name: ");
                scanf("%s",i.iname);
                printf("\nQuantity: ");
                scanf("%d",&i.quantity);
                printf("\nPrice: ");
                scanf("%f",&i.price);
                fseek(fp,-recsize,SEEK_CUR);
                fwrite(&i,recsize,1,fp);
                break;
                    }
                      //    else{
                    //printf("\n Invalid Item code");
                       //   }
                      }
                      rewind(fp);
                 printf("\nSuccessfully Modified");
                 printf("\nDo u want to modify more record (y/n)");
                 fflush(stdin);
                 another=getche();
    

    } }

Edited by Reverend Jim: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.