Thread: Binary file trouble

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    4

    Binary file trouble

    Hi, I hope someone here can shed some light on a problem I'm having.

    The simple program below reads a registry number and a name associated with it from the keyboard.

    Upon exiting, all of the file contents are printed to the screen, this is working fine.

    Accessing a specific name using the registry number doesn't work for some reason but for the life of me I can't figure it out.
    Hope someone can take a look and help. Thanks.


    Code:
    #include <conio.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <cstdlib>
    
    using namespace std;
    
    class info {
          private :
                  int regnum,status;
                  char name[50];
          public : 
                 int checkreg(int);
                 void write_name(int);
                 void read_name();
                 void print_all();
                 };
    fstream A;
    
    info names;
    
    int info::checkreg(int n)
    {
        A.open("c:\\names.dat",ios::in|ios::binary);
        A.clear();
        if(A.good())
        {
         
        int flag=1;
        while(A.read((char*)&names,sizeof(info))&&n!=regnum);
         if(n==regnum&&status)
          flag=0;
         A.close();
         return flag;
    }
    else cout<<"\nUnable to open file for checkreg";
    
    }
    
    void info::write_name(int n)
    {
     A.open("c:\\names.dat",ios::out|ios::binary|ios::app);
     A.clear();
     if(A.good())
      {
     
     regnum=n;
     cout<<"\nEnter Name : ";
     cin>>name;
     status=1;
     A.write((char*)&names,sizeof(info));
     A.close();
     cout<<"\nRegistry saved to file";
    }
    else cout<<"\nUnable to open file for write";
    }
    
    void info::read_name()
    {
     A.open("c:\\names.dat",ios::in|ios::binary);
     A.clear();
     if(A.good())
      {
      
    
     int num;
     cout<<"\nEnter the registry number : ";
     cin>>num;
     
     while(A.read((char*)&names,sizeof(info))&&num!=regnum)
     {
      if(num==regnum&&status)
      {                     
       cout<<"Name : "<<name;
       
    }
     
      else cout<<"\nRegistry not found";
     }
     A.close();
    }
    else cout<<"\nUnable to open file to read";
    }
    
    void info::print_all()
    {
     A.open("c:\\names.dat",ios::in|ios::binary);
     while(A.read((char*)&names,sizeof(info)))
     {
      cout<<"\n\n";
      cout<<"\nRegnum = "<<regnum;                                        
      cout<<"\nName = "<<name;
     
    }
    }
    
    int main(void)
    {
      int num;
      char option;
      do{
           system("cls"); 
           cout<<"\na - Write to file : ";
           cout<<"\nb - Check a registry : ";
           cout<<"\nc - exit and output all entries : ";
           cout<<"\n\nEnter a b or c: ";
           option=getche();
           switch(option)
           {
                         case 'a':
                              do{
                                  cout<<"\nEnter registry number : ";
                                  cin>>num;
                                  if(names.checkreg(num))
                                  {
                                   names.write_name(num);
                                   
                                   }
                                  else cout<<"\nThis registry already exists";
                                  cout<<"\nWould you like to enter another name? (y/n)";
                                  }while(getche()!='n');
                                  cout<<"\n";
                                 getch();
                                  break;
                                  
                         case 'b' : names.read_name();
                                    cout<<"\n";
                                    getch();
                                    break;
                         case 'c' : break;
                         default : cout<<"\nThis option doesnt exist";
                         }
                         }while(option!='c');
                         
                         names.print_all();
                         getch();
                         return 0;
    }

  2. #2
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    The problem specifically in the member function : void info::read_name(), can't get the screen output here.

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    What compiler are you using?? Your program will not compile with VC++ 6.0 because you are mixing obsolete header files with "using namespace std;". Remove the .h extension from iostream and fstream.

    you can get rid of conio.h, which isn't portable anyway. replace getche() with cin.get().

    Accessing a specific name using the registry number doesn't work for some reason
    The while loop is incorrect. try something like this
    Code:
    void info::read_name()
    {
     A.open("c:\\names.dat",ios::in|ios::binary);
     A.clear();
     if(A.is_open())
     {
      
    
     int num;
     bool found = false;
     cout<<"\nEnter the registry number : ";
     cin>>num;
     cout << endl; 
     while(A.read((char*)&names,sizeof(info)) && !found)
     {
      if(num==regnum)
      {                     
       cout<<"Name : "<<name;
       found = true;
      }
     
     }
     A.close();
     if(!found)
     {
      cout<<"Registry not found" << endl;
     }
    }
    else cout<<"\nUnable to open file to read";
    cout << endl << "Press <Enter> when ready to continue" << endl;
    cin.ignore();
    }

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    4
    Thanks for taking the time to read my code Ancient Dragon, I'll give it a shot. I'm using Dev C++by the way


    -Just tried it, works like a charm, many thanks Ancient
    Last edited by bamera; 10-14-2005 at 01:15 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  4. Simple File encryption
    By caroundw5h in forum C Programming
    Replies: 2
    Last Post: 10-13-2004, 10:51 PM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM