help to detect errors in this simple proggramme

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nilushika
    New Member
    • Sep 2013
    • 20

    help to detect errors in this simple proggramme

    help to detect errors in this simple proggramme

    Code:
      	
        #include<stdio.h>
        #include<ctype.h>
        #include<string.h>
        struct customer readCustomer();
        struct slot readSlot();
        struct slot{int day,period;};
        struct customer{
        char name[50],tel[10], paidOrNot[8];};
        struct booking{
            char freeOrNot;//if not free, gives 'n'....
            struct slot slot_;
            struct customer customer_;
        };
        struct bookingFile{
             struct booking array[7][10];   
        };
         
        main(){
            FILE *fp;
            struct bookingFile bf;
            struct slot s;
            fp=fopen("store.txt","r+");
            if(fp==NULL){
                printf("cannot open the file");
                exit(0);
            }
            fread(&bf,sizeof(struct bookingFile),1,fp);
        do{                //validating whether the given slot is free or not....
         
            s=readSlot();
            if(bf.array[s.day][s.period].freeOrNot=='n')
            printf("Sorry,the slot you gave has been already booked.Try another slot!\n");
        }while(bf.array[s.day][s.period].freeOrNot=='n');
         
        bf.array[s.day][s.period].freeOrNot=0;
        bf.array[s.day][s.period].slot_=s;
        bf.array[s.day][s.period].customer_=readCustomer();
        rewind(fp);
        fwrite(&bf,sizeof(struct bookingFile),1,fp)    ;
        rewind(fp);   
                printf("%s\n",bf.array[0][0].customer_.tel);//line a
                printf("%s\n",bf.array[1][1].customer_.tel);//line b
                printf("%s\n",bf.array[2][2].customer_.tel);//line c
                printf("%s\n",bf.array[3][3].customer_.tel);//line d
                fclose(fp);   
        }
         
        struct customer readCustomer(){
            struct customer c;
         
            printf("Enter your name: ");
            gets(c.name);
            printf("Enter the telephone number: ");
            gets(c.tel);
            printf("Are you paying ,just now?(Yes/No):");
            gets(c.paidOrNot);
         
            return c;
        }
        struct slot readSlot(){
            struct slot s;
            printf("Enter the number of day you want\n1.mon\n2.tues\n3.wed\n4.thu\n5.fri\n6.sat\n7.sun: ");
            scanf("%i",&s.day);
            printf("Enter the number of period you want\n1.8-9\n2.9-10\n3.10-11\n4.11-12\n5.12-1\n6.1-2\n7.2-3\n8.3-4\n9.4-5\n10.5-6: ");
            scanf("%i",&s.period);
            return s;
        }


    This code compiles fine.But
    the data I tried to save to the file,' store.txt' are not permanantly stored. i could assert it by running this code block several times(more than 4), by the output produced by line a,b,c&d.
    Can you plese help me to detect the reasons for why it doesn't get stored them permanantly.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This code:

    Code:
    rewind(fp);
    fwrite(&bf,sizeof(struct bookingFile),1,fp)    ;
    rewind(fp);
    positions your file to the beginning before writing, So all of your data gets written on top of previous data at the beginning of the file.

    You should use fseek(SEEK_END) before writing.

    Comment

    • nilushika
      New Member
      • Sep 2013
      • 20

      #3
      what I just wanted in this progrrame is that.it means i want to modify the array each time & restore it .so data gets written on top of previous data at the beginning of the file is not a problem.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        So what is in store.txt after you run the program?

        Comment

        • nilushika
          New Member
          • Sep 2013
          • 20

          #5
          I want to store the finally modified array in the store.txt.


          In the each time this code run I read the previosly stored array and append new data to it.
          for example think that in the previous session I have filled the array cell 1.then next session I fill the array cell 2.

          Like that I continued the filling array,But previously stored data should remained,(altho ugh it overriden the array each time:because previously stored array is read in the current session & modify it & rewrite it in the file)shoudn't that?

          But it doesn't happen as I supposed.Previo usly stored data to the array sometimes get vanished.

          Comment

          • nilushika
            New Member
            • Sep 2013
            • 20

            #6

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              This is the time you need to start stepping through the code using your debugger. Embedded printf statements are not a good debugging method.

              If you don't know how to use your debugger, then this is a great opportunity t learn it. Most debuggers are very easy to use and they let you see the values in your variables each step of the way. Just start at the beginning of main() and step through the code a line at a time. Be sure to step into each of your functions. Verify the variables have correct values at all times.

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                Line 32/34 you check freeOrNot to see if it is 'n' to indicate if the slot is taken but at line 36 you set freeOrNot to 0 when actually writing a record.

                You have named the file storer.txt which suggests you think it is a text file but you use fread/fwrite which read/write binary data.

                Comment

                • nilushika
                  New Member
                  • Sep 2013
                  • 20

                  #9
                  yes Banfa,you are correct.this is a mistake happened when I post it.result is same even after 0 replaced by n.
                  can't we actually use fread/fwrite for text files?then how can i fix it?what should be the extention?

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    All files are binary files.

                    A "text file" is just a binary file where each byte is an ASCII character between 0 and 127. The normal process is to unpack struct members into arrays of a fixed length and then write the array to the file. You need to know the order in which the arrays were written in order to read them back.

                    When you read the arrays back you pack the arrays back into a struct. Since you know the order in which they were written, you know how to load the struct

                    Often and "record mark". Usually a \n, but sometimes a \0, is written to indicate the end of a piece of data. In cases like this you can read the data in a file as strings.

                    The name of the file has no connection to the data in the file.

                    Comment

                    Working...