Technical Help & Discussion > Website Design & Programming

c++ binary file ops

<< < (2/4) > >>

davypipes:
ok!!! thats it....i really need someone with a good idea on how am to edit the farmer records!!!!rather ...how am supposed to edit a farmer's details,eg name...location..acc number!!!
 heres what ive been able to do so far!!!!
#include&lt;fstream.h&gt;
#include&lt;conio.h&gt;
#include&lt;iomanip.h&gt;
#include&lt;stdio.h&gt;
/*

*************************************************************
program description
*************************************************************

this is  a program for storing farmers records
the functions include:
registering farmers
searching for a specific farmer
viewing farmer records!
*************************************************************
*/
class Farmer
{
  char farmerName[20];
  long idNumber;
  long accountNumber;
  char location[30];

  public:
&nbsp;&nbsp;&nbsp;void mainMenu();
&nbsp;&nbsp;&nbsp;void registerFarmer();
&nbsp;&nbsp;&nbsp;void searchFarmer();
&nbsp;&nbsp;&nbsp;void viewFarmer();
&nbsp;&nbsp;&nbsp;//void editfarmer();

};
void waitmsg()
{
&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;PRESS RETURN [enter]TO EXIT&quot;;
&nbsp;&nbsp;&nbsp;getchar();
}

//procedure for printing a record
 void Farmer::viewFarmer()
  {

&nbsp;&nbsp;&nbsp;int i=1;
&nbsp;&nbsp;&nbsp;ifstream myfile(&quot;Farmers.dat&quot;,ios::in) ;
&nbsp;&nbsp;&nbsp;clrscr();
&nbsp;&nbsp;&nbsp; cout&lt;&lt;&quot;         LIST OF ALL REGISTERED FARMERS \n\n&quot;&lt;&lt;endl;
&nbsp;&nbsp;&nbsp; cout&lt;&lt;&quot; &quot;&lt;&lt;setw(5)&lt;&lt;&quot;FARMER NAME&quot;&lt;&lt;setw(17)&lt;&lt;&quot;ID NUMBER&quot;&lt;&lt;setw(17)&lt;&lt;&quot;ACCOUNT NUMBER&quot;&lt;&lt;setw(17)&lt;&lt;&quot;LOCATION&quot;&lt;&lt;setw(17)&lt;&lt;endl;
&nbsp;&nbsp;&nbsp; cout&lt;&lt;&quot;-----------------------------------------------------------------------------&quot;&lt;&lt;endl&lt;&lt;endl;
&nbsp;&nbsp;&nbsp; while(myfile&gt;&gt;farmerName&gt;&gt;idNumber&gt;&gt;accountNumber&gt;&gt;location)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;i&lt;&lt;&quot;   &quot;&lt;&lt;setw(7)&lt;&lt;farmerName&lt;&lt;setw(18)&lt;&lt;idNumber&lt;&lt;setw(17)&lt;&lt;accountNumber&lt;&lt;setw(17)&lt;&lt;location&lt;&lt;setw(17)&lt;&lt;endl&lt;&lt;endl;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;i++;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;&quot;\n-------------------------END OF RECORDS----------------------------&quot;&lt;&lt;endl;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfile.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;waitmsg();
&nbsp;&nbsp;&nbsp;}



void Farmer::registerFarmer()
{
&nbsp;&nbsp;&nbsp;ofstream myfile(&quot;Farmers.dat&quot;,ios::app);
&nbsp;&nbsp;&nbsp;clrscr();

&nbsp;&nbsp;&nbsp;char choice ='y';
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while (choice=='y' || choice=='Y')
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clrscr();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\tYOU HAVE CHOSEN TO REGISTER A NEW FARMER \n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t----------------------------------------\n\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;Please type in the details for the farmer \n\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;Farmer Name :&quot;;    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin &gt;&gt;farmerName;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;\nFarmer ID Number :&quot;;&nbsp;&nbsp;&nbsp;   cin &gt;&gt;idNumber;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;\nFarmer Account Number :&quot;; cin  &gt;&gt;accountNumber;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;\nPhysical Location :&quot;;    cin  &gt;&gt;location;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//save the entered data to file*/
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfile&lt;&lt;farmerName&lt;&lt;&quot; &quot;&lt;&lt;idNumber&lt;&lt;&quot; &quot;&lt;&lt;accountNumber&lt;&lt;&quot; &quot;&lt;&lt;location&lt;&lt;&quot; \n&quot;  ;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;Dou you want to enter another record Y(es) N(o) ?&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin  &gt;&gt; choice;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfile.close();
&nbsp;&nbsp;&nbsp; }

//function to edit the record..
//heres tthe problem area....
/*void editfarmer()
{

} */


/*procedure for searching a record */
void Farmer::searchFarmer()
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ifstream myfile(&quot;a:Farmers.dat&quot;,ios::in);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;long int searchkey;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clrscr();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\tYOU HAVE CHOSEN TO SEARCH FOR A FARMER \n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t----------------------------------------\n\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;Please enter the account number of the farmer : &quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt; searchkey;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while((myfile&gt;&gt;farmerName&gt;&gt;idNumber&gt;&gt;accountNumber&gt;&gt;location) &amp;&amp; (accountNumber != searchkey))
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {  }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  if(accountNumber == searchkey)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout &lt;&lt;&quot;\n Farmer Name : &quot;&lt;&lt;farmerName&lt;&lt;&quot;\nFarmer ID Number : &quot;&lt;&lt;idNumber&lt;&lt;&quot;\nFarmer Account Number : &quot;&lt;&lt;accountNumber&lt;&lt;&quot;\nFarmer Location : &quot;&lt;&lt;location&lt;&lt;endl&lt;&lt;endl;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  else
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  cout&lt;&lt;&quot;\n \t \t Record not found !\n\n\n\n\n&quot; ;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\n-------------------------END OF RECORDS----------------------------&quot;&lt;&lt;endl;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;myfile.close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;waitmsg();
&nbsp;&nbsp;&nbsp; }
//Definition of function mainMenu
//Displays the main menu and asks the user to select


void Farmer::mainMenu(void)
{
&nbsp;&nbsp;&nbsp;int Choice;

&nbsp;&nbsp;&nbsp;do
&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;clrscr();

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\n\n\tWELCOME TO THE WATAMU DAIRY FARMERS COOPERATIVE SOCIETY\n\n\n\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|- - - - - - - - - - - - - - - -|\n&quot; ;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|\tTAMU DAIRY FARMERS\t|\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|-------------------------------|\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|\t  FARMERS RECORDS\t|\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|\t1.Register New Farmer\t|\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|\t2.View Farmer Records\t|\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|\t3.Search Farmer Record\t|\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|\t4.edit Farmer Record\t|\n&quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\t\t|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|\n\n &quot; ;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;&quot;\tEnter 1 , 2 , 3 , 4 , 5 or 6 :  &quot;;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt;Choice;


&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;//when the choice is not among the options
&nbsp;&nbsp;&nbsp;while (Choice &lt; 1 || Choice &gt; 4);



&nbsp;&nbsp;&nbsp;switch (Choice)
&nbsp;&nbsp;&nbsp; {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 1 :registerFarmer();mainMenu()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 2 :viewFarmer();mainMenu() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;;break;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case 3 :searchFarmer();mainMenu() &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;;break;
&nbsp;&nbsp;&nbsp;  // case 4:editfarmer()mainmenu();break;
&nbsp;&nbsp;&nbsp; }
}

void main()
{
&nbsp;&nbsp;&nbsp;Farmer fDetails;
&nbsp;&nbsp;&nbsp;fDetails.mainMenu();
}

Simon:
Hopefully Dack will be along soon to help you with that!   :o

Dack:
*LOTS* to do here I'm afraid :( First question is what C++ compiler are you using - clrscr isn't a standard MS function you see but is in Borland C.

Lets start with the easy ones:

1. Your menu function is recursive which would lead to memory errors. There is also no way of exiting the program safely.

Your code:

--- Quote ---void Farmer::mainMenu(void)
{
   int Choice;

   do
   {
      clrscr();

      cout&lt;&lt;&quot;\n\n\tWELCOME TO THE WATAMU DAIRY FARMERS COOPERATIVE SOCIETY\n\n\n\n&quot;;
      cout&lt;&lt;&quot;\t\t|- - - - - - - - - - - - - - - -|\n&quot; ;
      cout&lt;&lt;&quot;\t\t|\tTAMU DAIRY FARMERS\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|-------------------------------|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t  FARMERS RECORDS\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t1.Register New Farmer\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t2.View Farmer Records\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t3.Search Farmer Record\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t4.edit Farmer Record\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|\n\n &quot; ;
      cout&lt;&lt;&quot;\tEnter 1 , 2 , 3 , 4 , 5 or 6 :  &quot;;
      cin&gt;&gt;Choice;


   }
   //when the choice is not among the options
   while (Choice &lt; 1 || Choice &gt; 4);



   switch (Choice)
    {
      case 1 :registerFarmer();mainMenu()      ;break;
      case 2 :viewFarmer();mainMenu()        ;break;
      case 3 :searchFarmer();mainMenu()        ;break;
     // case 4:editfarmer()mainmenu();break;
    }
}
--- End quote ---


This works better if written as:

--- Quote ---void Farmer::mainMenu(void)
{
   int Choice = 0;
   do
   {
     do
     {
      clrscr();

      cout&lt;&lt;&quot;\n\n\tWELCOME TO THE WATAMU DAIRY FARMERS COOPERATIVE SOCIETY\n\n\n\n&quot;;
      cout&lt;&lt;&quot;\t\t|- - - - - - - - - - - - - - - -|\n&quot; ;
      cout&lt;&lt;&quot;\t\t|\tTAMU DAIRY FARMERS\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|-------------------------------|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t  FARMERS RECORDS\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t1.Register New Farmer\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t2.View Farmer Records\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t3.Search Farmer Record\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t4.edit Farmer record\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|\t5.Exit program\t\t|\n&quot;;
      cout&lt;&lt;&quot;\t\t|_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|\n\n &quot; ;
      cout&lt;&lt;&quot;\tEnter 1 , 2 , 3 , 4 , 5 or 6 :  &quot;;
      cin&gt;&gt;Choice;


     }
     //when the choice is not among the options
     while (Choice &lt; 1 || Choice &gt; 5);



   switch (Choice)
    {
      case 1 :registerFarmer();break;
      case 2 :viewFarmer();break;
      case 3 :searchFarmer();break;
     // case 4:editfarmer()mainmenu();break;
     default: break;
    }
   }while (Choice != 5);
}
--- End quote ---

Dack:
2. Your registerfarmer routine doesn't check to see if the number already exists in the file - if so you should say that there is a duplicate entry.

--- Quote ---void Farmer::registerFarmer()
{
   ofstream myfile(&quot;Farmers.dat&quot;,ios::app);
   clrscr();

   char choice ='y';
      while (choice=='y' || choice=='Y')
        {
            clrscr();
            cout&lt;&lt;&quot;\t\tYOU HAVE CHOSEN TO REGISTER A NEW FARMER \n&quot;;
            cout&lt;&lt;&quot;\t\t----------------------------------------\n\n&quot;;
            cout &lt;&lt;&quot;Please type in the details for the farmer \n\n&quot;;
            cout &lt;&lt;&quot;Farmer Name :&quot;;             cin &gt;&gt;farmerName;
            cout &lt;&lt;&quot;\nFarmer ID Number :&quot;;     cin &gt;&gt;idNumber;
            cout &lt;&lt;&quot;\nFarmer Account Number :&quot;; cin  &gt;&gt;accountNumber;
            cout &lt;&lt;&quot;\nPhysical Location :&quot;;    cin  &gt;&gt;location;

            //save the entered data to file*/
            myfile&lt;&lt;farmerName&lt;&lt;&quot; &quot;&lt;&lt;idNumber&lt;&lt;&quot; &quot;&lt;&lt;accountNumber&lt;&lt;&quot; &quot;&lt;&lt;location&lt;&lt;&quot; \n&quot;  ;

            cout &lt;&lt;&quot;Dou you want to enter another record Y(es) N(o) ?&quot;;
            cin  &gt;&gt; choice;
        }
      myfile.close();
    }
--- End quote ---


Should be:

--- Quote ---void Farmer::registerFarmer()
{

   clrscr();

   char choice ='y';
      while (choice=='y' || choice=='Y')
        {
            clrscr();
            cout&lt;&lt;&quot;\t\tYOU HAVE CHOSEN TO REGISTER A NEW FARMER \n&quot;;
            cout&lt;&lt;&quot;\t\t----------------------------------------\n\n&quot;;
            cout &lt;&lt;&quot;Please type in the details for the farmer \n\n&quot;;
            cout &lt;&lt;&quot;Farmer Name :&quot;;             cin &gt;&gt;farmerName;
            cout &lt;&lt;&quot;\nFarmer ID Number :&quot;;     cin &gt;&gt;idNumber;
            cout &lt;&lt;&quot;\nFarmer Account Number :&quot;; cin  &gt;&gt;accountNumber;
            cout &lt;&lt;&quot;\nPhysical Location :&quot;;    cin  &gt;&gt;location;

         // See if he already exists
         if (false==SearchForExisting(accountNumber))
         {
            //save the entered data to file
            ofstream myfile(&quot;Farmers.dat&quot;,ios::app);
            myfile&lt;&lt;farmerName&lt;&lt;&quot; &quot;&lt;&lt;idNumber&lt;&lt;&quot; &quot;&lt;&lt;accountNumber&lt;&lt;&quot; &quot;&lt;&lt;location&lt;&lt;&quot; \n&quot;  ;
            myfile.close();
         }
         else
         {
            cout &lt;&lt; &quot;\nFarmer already exists&quot;;
            cout &lt;&lt; &quot;\n&quot; &lt;&lt; farmerName&lt;&lt;&quot; &quot;&lt;&lt;idNumber&lt;&lt;&quot; &quot;&lt;&lt;accountNumber&lt;&lt;&quot; &quot;&lt;&lt;location&lt;&lt;&quot; \n&quot;  ;
         }
            cout &lt;&lt;&quot;Do you want to enter another record Y(es) N(o) ?&quot;;
            cin  &gt;&gt; choice;
        }
    }
/*procedure for searching a record */
void Farmer::searchFarmer()
{

      long int searchkey;

     //      clrscr();
      cout&lt;&lt;&quot;\t\tYOU HAVE CHOSEN TO SEARCH FOR A FARMER \n&quot;;
      cout&lt;&lt;&quot;\t\t----------------------------------------\n\n&quot;;
      cout&lt;&lt;&quot;Please enter the account number of the farmer : &quot;;
      cin&gt;&gt; searchkey;

     if (true==SearchForExisting(searchkey))
     {
      cout &lt;&lt;&quot;\n Farmer Name : &quot;&lt;&lt;farmerName&lt;&lt;&quot;\nFarmer ID Number : &quot;&lt;&lt;idNumber&lt;&lt;&quot;\nFarmer Account Number : &quot;&lt;&lt;accountNumber&lt;&lt;&quot;\nFarmer Location : &quot;&lt;&lt;location&lt;&lt;endl&lt;&lt;endl;
      }
      else
        cout&lt;&lt;&quot;\n \t \t Record not found !\n\n\n\n\n&quot; ;

      cout&lt;&lt;&quot;\n-------------------------END OF RECORDS----------------------------&quot;&lt;&lt;endl;


      waitmsg();
}

// New function that searches for an existing record and reads the values
// into the buffer if we already have them
bool Farmer::SearchForExisting(long nSearchKey)
{
&nbsp;&nbsp;&nbsp;// Initially show that we have not found the searchfield
&nbsp;&nbsp;&nbsp;bool bRetVal = false;
&nbsp;&nbsp;&nbsp;long nLocalAccountNumber = -1;
&nbsp;&nbsp;&nbsp;long nLocalIDNumber = -1;
&nbsp;&nbsp;&nbsp;char LocalFarmerName[20];
    char LocalLocation[30];
&nbsp;&nbsp;&nbsp;// Open the file for reading
&nbsp;&nbsp;&nbsp;ifstream myfile(&quot;C:Farmers.dat&quot;,ios::in);


    while((myfile&gt;&gt;LocalFarmerName&gt;&gt;nLocalIDNumber&gt;&gt;nLocalAccountNumber&gt;&gt;LocalLocation)
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;   &amp;&amp; (nLocalAccountNumber != nSearchKey))
       {  };

&nbsp;&nbsp;&nbsp;// indicate in the return that we have found the value
    // and update the values in out local record
    if(nLocalAccountNumber == nSearchKey)
    {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;idNumber = nLocalIDNumber;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;accountNumber = nLocalAccountNumber;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strncpy(farmerName, LocalFarmerName, 20);
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;strncpy(location, LocalLocation, 30);

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;bRetVal = true;
&nbsp;&nbsp;&nbsp;}
&nbsp;&nbsp;&nbsp;// Close the file
&nbsp;&nbsp;&nbsp;myfile.close();
&nbsp;&nbsp;&nbsp;return bRetVal;
}

--- End quote ---


The searchforexisting function will be useful in a minute :)

Dack:
If you put those routines in then you'll see the problem I was on about concerning using write and read for the stream processing. If you open the farmers.dat file you'll see that each record you entered was a different size (because of the length of the text fields). You should now realise that you have to make all records the same (maximum size) in case you have to edit one and the new data will take up more space in the file than the existing data.

This involves simply changing the file input and output routines to use the read/write commands. We will then know how big each record is (20 bytes + the size of a long + the size of a long plus 30 bytes)

The edit routine then comes down to:
1. Get search key
2. find record that key matches
3. update fields in memory
4. open file for reading and writing
5. find place in file to change
6. write data
7. close file.

To make a read/write file you need to use the fstream type and the tellg and seekp operators.

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version