include<stdlib.h>
include<stdio.h>
include<string.h>
 struct D
 {   
     char name[20];
     float balance;
 }d;


 struct C
 {   
   char name[20];
    int count;
 }c;
 char ch;

    struct A
    {
 char command[20];
 char name[20];
 char filename[25];
 float amount;
    }a;
 struct P
 {
   char name[20];
 }p;

 int main(char argc, char**argv)
{    
struct A b;
char str[100];
FILE *fp,*fp1,*fp2;
int ch,i, choice,c,pref,m;
char user_name[100];
char i1;
float credit_amount,debit_amount;
printf("\n Enter the mode you would would like to operate on:\n");
printf("\n1.File Mode \n");
printf("\n2. Shared Memory mode\n");
scanf("%d",&ch);

switch(ch)
{

    case 1:     
            Label:            printf("\n You've Chosen File mode ... \n");
            printf("\n Do you wish to login(1) or exit(0)\n");
            scanf("%d",&c);
            if(c == 1)
            {
                printf("\n Enter the user name:\t");
                printf("\n");
                scanf("%s",&user_name);
                printf("%s",user_name);
                printf("Logging in\n");
                while(1)
                {
                    printf("food");
                    fp = fopen("command.txt","w");
                    printf("hello");
                    if(fp == NULL)
                    {
                            printf("world");
                    //        continue;
                    }
                    strcpy(b.command,"CREATE");
                    strcpy(b.name,user_name);
                    fwrite(&b,sizeof(struct A),1,fp);
                    printf("%s",b.name);
                    fclose(fp);
                    break;                                             // just in case so that server has enough
                                                                   //  time to read before client closes the file                
                }
             }    
            else
            {
                exit(0);
            }



            fp1 = fopen("s.txt","r");
                if(fp1 == NULL)
                {
                printf("\n Error in opening file\n");
                    exit(0);

                }

            while (1)
            {    
                //sleep(5);
                if( (ch = getc(fp1))==EOF)
                {
                    continue;
                }

                else
                {
                    fseek(fp1, 0, SEEK_SET);
                        for(  i= 0; i<30; i++)
                            {    
                                if((ch = getc(fp1)) == EOF)
                                {
                                    b.filename[i] = '\0';
                                    break;
                                }

                                b.filename[i] = ch;

                            }    

                    //    strcat(File_Array,".txt");

                 }


                break;                                        // While(1): Break
               }    




            fclose(fp1);
            //fclose(fp);
            repeat:            printf("\n\n\t\t MENU\n\n");
            printf("\n1.CREDIT\n");
            printf("\n2.DEBIT\n");
            printf("\n3.BALANCE\n");
            printf("\n4.LOGOUT\n");
            printf("\nEnter your choice  :  ");
            scanf("%d",&pref);

            switch(pref)
            {

                case 1:

                    printf("\nEnter the Amount to be Credited :  ");
                    scanf("%f", &credit_amount);
                    fp = fopen("command.txt","w");
                    strcpy(b.command,"CREDIT");
                    b.amount = credit_amount;
                    strcpy(b.name,user_name);
                    fwrite(&b,sizeof(struct A),1,fp);
                    //sleep(2);
                    fclose(fp);                               // just in case so that server has enough
                    break;                                    //  time to read before client closes the file

                case 2:
                    printf("\n Enter the balance to be Debited :  ");
                    scanf("%f",&debit_amount);
                    //fp = fopen("command.txt","w");
                    strcpy(b.command,"DEBIT");
                    b.amount = debit_amount;
                    strcpy(b.name,user_name);
                    fwrite(&b,sizeof(struct A),1,fp);
                    //sleep(2);
                    fclose(fp);                               // just in case so that server has enough
                    break;                                    //  time to read before client closes the file



                case 3:     fp = fopen("command.txt","w");
                         strcpy(b.command,"BALANCE");
                         strcpy(b.name,user_name);
                         //sleep(2);
                         fwrite(&b, sizeof(struct A), 1, fp);
                         fclose(fp);                               // just in case so that server has enough
                         //  time to read before client closes the file


                         break;

                default: 
                         printf("\n Invalid choice ...\n\n\\n");
                         printf("\n Terminating ..\n\n\n");
                         /* int c=1, d=1;
                         for(c=1;c<=32767;c++)
                             for(d=1;d<=32767;d++)
                             {
                             }*/
                         //exit(0); 
                         goto repeat;
            }

            fp2 = fopen(b.filename,"r");
            /*while(1)
            {
                if(fp2 == NULL)
                {
                    continue;
                }
            }*/
            // mode changed here

            while(1)
            {

                    if(strcmp(b.command,"CREDIT")==0)
                    {    
                        /*char* ch_credit = "CREDITED";*

                        printf("%s\t %f\t %s\t",user_name,credit_amount);*/
                        while((i1=getc(fp2))!= EOF)
                        {
                            printf("%c",i1);

                        }    

                    }

                    else if(strcmp(b.command,"DEBIT"))
                    {
                        /*char* ch_debit = "DEBITED";
                        printf("%s\t %f\t %s\t",user_name,debit_amount,ch_debit);*/
                        while((i1=getc(fp2)) != EOF)
                        {
                            printf("%c",i1);
                        }
                    }

                    else if(strcmp(b.command,"BALANCE"))
                    {    
                               /*char* ch_balance = "BALANCE";
                               fgets(str, 100, fp2);*/
                                while((i1=getc(fp2)) != EOF)
                                {
                                    printf("%c",i1);
                                }
                    }


                        //printf("%s\t %s\t %s\t ",ch_balance,user_name,str);       //  <-----------  // what balance will come here


                break;                                            // while(1): Break 

            }

            break;                            


    case 2:
            printf("\n You've chosen Shared memory mode:\n");
            printf("\n OOPS, Due to technical problems you will be redirected to File mode..\n");
            printf("\n Do you wish to continue? press (1) to continue  or exit(any key):\n");
            scanf("%d", &choice);
            switch(choice)
            {

                case 1:     goto Label;
                            break;

                default:     
                            exit(0);
            }
            break;

    default: 
            printf("\n Invalid choice ...\n\n\\n");
            printf("\n Redirecting to file mode ..\n\n\n");
            /*int c=1, d=1;
            for(c=1;c<=32767;c++)
                for(d=1;d<=32767;d++)
                {
                }*/
            //exit(0);
            goto Label;


}

printf("do u want to continue,1 to continue");
scanf("%d",&m);
if(m ==1)
{
    goto repeat;
}
else
{
    exit(0);
}


return 0;
}    

Recommended Answers

All 3 Replies

I am getting segmentation fault when i am doing credit.
please help..

What lunatic taught you to use goto like that? Or at all, for that matter? If your professor is teaching to write code like that - hell, if you professor even mentioned goto in the first place - you should walk away from that course.

Here's a piece of advice: forget that the keyword goto exists. You should never, ever use it for ordinary flow control. It is far too difficult to debug code that uses it. While goto does have its place in C programming, someone just learning the language shouldn't touch it.

commented: Could not agree more! +15

As for the segfault, it appears to be occurring when you are trying to read from the b.filename file, if the file ins't actually there.

On an unrelated note, you cannot write a struct out to a file directly and expect it to be usable; you need to write out the individual members of the structure separately, a process called serialization. Here's a simple function to serialize your A struct values:

void write_command(struct A* cmd, FILE* fp)
{
    fprintf(fp, "%s,%s,%s,%f\n", cmd->command, 
        cmd->name, cmd->filename, cmd->amount); 
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.