problem in modifying data in a binary file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tracethepath
    New Member
    • Jun 2007
    • 15

    problem in modifying data in a binary file

    hello everybody...
    i am trying to make a project on binary files and that needs a function to modify data in a binary file. I have made the following class.

    Code:
    class example
    {
       public:   
    
       char name[10], number[5];
       void getdetails();
       void showdetails();
    
       example()
       {
          name[0]='\0', number[0]='\0';
       }
       
       char* getname()
       {
          return name;
       }
    
    };
    and the definition of mod_data() ( a function to modify data of the class ) is as follows:

    Code:
     
    void mod_data(example &abc)
    {
       char name[10], number;
       
       cout<<"NAME:  ";
       gets(name);
       if(strlen(name)!=0)
         strcpy(abc.name, name);
       
       cout<<"NUMBER:  ";
       gets(number);
       if(strlen(number)!=0)
         strcpy(abc.number, number);
    }
    to modify the record in the binary file, function modify is used

    Code:
     
    void modify()
    {
       int recc = 0;
       int flag=0;
       char name[25];
       example abc;
       clrscr();
       fstream fil ("EXAMPLE.dat",ios::in);   
       cout<<"\n  Enter The Name Of The Person Whose Details To Be Modified:  ";
       gets(name);
       cout<<endl;
    
       while (!fil.eof())
       { 
          fil.read ((char*)&abc,sizeof(abc));
          recc++;
          if (stricmp(abc.getname(), name)==0)
          {
              flag=1;
              break;
          }
       }
       
       fil.close();
       if(flag==1)
       {
       fil.open ("EXAMPLE.dat",ios::out|ios::ate);
       mod_data(abc);
       fil.seekp((recc-1)*sizeof(abc), ios::beg);
       fil.write((char*)&abc,sizeof(abc));
       fil.close();
       }
    
        else
        {
         cout<<"\n  PERSON NOT FOUND";
         getch();
        }
    }
    but the program is not working properly.
    in my original class there are more than 15 data members. And what i want to do is if the user presses enter in a particular field the record for that field which was present before remains as it is i.e. that field is not modified. But the function works properly for only the first few data members and not for the last ones.The last data members when displayed appear blank or garbage data is displayed. Please help me out.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    I suspect the format of the data on the disc does not match your sizeof calculations. Often, the compiler aligns data on integer or double boundaries.

    Rather than read/write data using sizeof, I recommend you write a Serialize() method that writes each data member to disc indivually in a pattern you can depend upon. Later, you read the data back using the same pattern you used for writing.

    You say the real class has 15 members an if any of them are other than char, then you could have this problem.

    Comment

    • tracethepath
      New Member
      • Jun 2007
      • 15

      #3
      Originally posted by weaknessforcats
      I suspect the format of the data on the disc does not match your sizeof calculations. Often, the compiler aligns data on integer or double boundaries.

      Rather than read/write data using sizeof, I recommend you write a Serialize() method that writes each data member to disc indivually in a pattern you can depend upon. Later, you read the data back using the same pattern you used for writing.

      You say the real class has 15 members an if any of them are other than char, then you could have this problem.
      what is this " Serialise()"??
      i have not read this yet...
      can you explain a bit further?
      how to call it n do read and write operations using it?

      and all my class members are of char type...
      by the way my problem is solved now...i made a tempory file and went on writing the data until the data to be modified occured modified it and copied rest of the data to the temp file...and then removed original file and renamed the temp...

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You need to define the format of your disc records.

        Your class has a name array of 15 and a number array of 5. plus 13 other members.

        I suggest using a flat file approach where each record has exactly the same layout.

        Each record has 15 bytes for the name followed by 20 bytes for the number.

        When you write, you write exactly 125 bytes for all names and 20 bytes for all numbers. Then you read back in using this exact format.

        This process is called serialization.

        You could use a script approach where everythign is written as text. In this case you write 15 characters for the name and then convert the number into a string of char. That string would need to hold the max value 9999 to -9999.

        On the read, you convert the chars back into an int.

        The advantage here is that you can edit your file with a text editor and that makes it easier to get data from other programs.

        Comment

        Working...