Structures and functions when writing/reading to file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ConfusedAlot
    New Member
    • Mar 2007
    • 17

    Structures and functions when writing/reading to file

    I am having difficulty defining the function prototypes and corresponding parameters when the function is called from main. Any help is greatly appreciated.

    Code:
    //The database
    
    struct OpAmps {
      char Name[20]; 
      unsigned int PinCount; 
      double SlewRate;
    
    //The function prototypes
    
    void Enter(OpAmps );
    int Save(OpAmps );
    void Load(OpAmps );
    void Sort(OpAmps );
    void Display(OpAmps );
    
    //The arguments to be passed from main, where database_length is an unsigned long and OpAmp is of type OpAmps
    
        Enter(OpAmp[database_length], database_length);
            break;
          
          case '2':
            Save(OpAmp, database_length);
            break;
          
          case '3':
            Load(OpAmp, database_length);
            break;
          
          case '4':
            Sort(OpAmp, database_length);
            break;
          
          case '5':
            Display(OpAmp, database_length);
    
    //the functions
    
    void Enter(OpAmp, int length&);
    {
    // if the database is full, inform the user
    
    	if (length=10)
    	{
    		cerr << "The database is full" << endl;
    	}
    
    // if the database is not full, get the data from the user and alter the database length
    
    	else cout << "Enter Operational Amplifier name: ";
    	cin >> OpAmp.Name;
    	cout << endl << "Enter number of pins: ";
    	cin >> OpAmp.PinCount;
    	cout << endl << "Enter slew rate: ";
    	cin >> OpAmp.SlewRate;
    	cout << endl;
    	length++;
      
    }
    
    
    void Open(OpAmp, int length&);
    {  
      fstream output_file;  // file stream for output
    
      // open the file
      //<enter code here>
      output_file.open(DATABASE_FILENAME, ios::out);
    
      // write length information to file
      //<enter code here>
    
      // write data to file
      //<enter code here>
      output_file << Name << PinCount << SlewRate << endl;
    
      // close the file
      output_file.close();
    }
    
    
    void Load(OpAmp, int length&);
    {  
      fstream input_file;  // file stream for input
    
      // open the file
      //<enter code here>
      input_file.open(DATABASE_FILENAME, ios::in);
    
      // load database length information from file
      //<enter code here>
      input_file.seekg(0*sizeof(int), ios::beg);
    
      // load data from file
      //<enter code here>
      int NumberRead;
      for (int i=0; i=length; i++)
      {
    	  input_file >> NumberRead
      
      // close the file
      input_file.close();
    
    void Display(OpAmp, int length&)
    {
    	int i = 0
      // if the database is empty, inform the user
      if (length=0)
      {
    	  cout << "The database is empty";
      }
      // if the database is not empty, display all the elements in the database
      //<enter code here>
      else
      {
    	  for (i=1;i=length;i++)
    	  {
    		  cout << OpAmp[i].Name << OpAmp[i].PinCount << OpAmp[i].SlewRate << endl;
    	  }
      }
    }
    
    
    //Sort is not defined at all yet, need help with the passing of the struct and the use of call by value/reference to change the value of database length as the struct entrys increase
    Last edited by sicarie; Mar 11 '07, 11:16 PM. Reason: Added [ code] and [ /code] tags (without the spaces)
  • DeMan
    Top Contributor
    • Nov 2006
    • 1799

    #2
    I think your greatest difficulty may be that you are trying to pass OpAmps through various functions. (and you seem to be missing at least one })

    If you have only one of these structs (that is you want a "global(ish )" struct, you can make a static struct, and you don't need to pass it anywhere.

    Otherwise, you can create an instance of the struct, but then you would need to be passing a variable name through to the methods

    eg:
    Code:
    void Enter(OpAmp myOpAmp, int length&);
    {
    // if the database is full, inform the user
    
    	if (length=10)
    	{
    		cerr << "The database is full" << endl;
    	}
    
    // if the database is not full, get the data from the user and alter the database length
    
    	else cout << "Enter Operational Amplifier name: ";
    	cin >> myOpAmp.Name;
    	cout << endl << "Enter number of pins: ";
    	cin >> myOpAmp.PinCount;
    	cout << endl << "Enter slew rate: ";
    	cin >> myOpAmp.SlewRate;
    	cout << endl;
    	length++;
      
    }

    Comment

    • ConfusedAlot
      New Member
      • Mar 2007
      • 17

      #3
      Thankyou, when i debug the code my main error is when passing the struct into the Save, Load and Display functions, i get the error, cannot convert parameter 1 from OpAmps[10] to 'OpAmps' how would i correct this?

      Comment

      • DeMan
        Top Contributor
        • Nov 2006
        • 1799

        #4
        Have yougpt ypour implementation of Save Handy?

        Comment

        • ConfusedAlot
          New Member
          • Mar 2007
          • 17

          #5
          This is the function prototype for Save

          Code:
          int Save(OpAmps, unsigned long);
          This is the portion in main where save is called

          Code:
                case '2':
                  Save(OpAmp, database_length);
                  break;
          This is the function so far

          Code:
          // Save the database to the file specified by DATABASE_FILENAME. If the file exists it
          // simply overwritten without asking the user
          // Arguments:
          //   (1) the database
          //   (2) the length of the database
          // Returns: void
          //<enter code here>
          int Save(OpAmps OpAmp, unsigned long length)
          {  
            fstream output_file;  // file stream for output
          
            // open the file
            //<enter code here>
            output_file.open(DATABASE_FILENAME, ios::out);
          
            // write length information to file
            //<enter code here>
          
            // write data to file
            //<enter code here>
            output_file << OpAmp.Name << OpAmp.PinCount << OpAmp.SlewRate << endl;
          
            // close the file
            output_file.close();
          }
          Last edited by Ganon11; Mar 12 '07, 01:12 PM. Reason: code tags added

          Comment

          • Ganon11
            Recognized Expert Specialist
            • Oct 2006
            • 3651

            #6
            Here's one problem:

            Your comments say the function returns void, but it is an int function. You should declare the function as void Save, or return a dummy value of 0.

            Comment

            • ConfusedAlot
              New Member
              • Mar 2007
              • 17

              #7
              Ive already changed that in the code im working on, took me a while to realise it, my main problem is the error code i referred to in an early post, ive tried a few combinations of arguments to be passed but not getting anywhere

              Comment

              • DeMan
                Top Contributor
                • Nov 2006
                • 1799

                #8
                I am guessing (by the Error) that you are trying to pass an array of opAmps into the function (but the function only expects a single instance). If you are trying to pass an array into the method, it needs to be declared to accept an array, so the simplest thing to do would be to to show the method parameters as

                int Save(OpAmps[] OpAmp, int length);

                Comment

                • ConfusedAlot
                  New Member
                  • Mar 2007
                  • 17

                  #9
                  Having problems with the save function, the debugger gets down to the cout line for opening file and then just stops, i think i need to declare a pointer to the structure but then i get errors in the lines that write the name etc to the file
                  Code:
                  void Save(OpAmps OpAmp[], unsigned long length)
                  {  
                    fstream output_file;  // file stream for output
                  
                    // open the file
                    //<enter code here>
                    output_file.open(DATABASE_FILENAME, ios::out);
                    if (!output_file.good())
                    {
                  	  cout << "Could not create file";
                  	  exit (1);
                    }
                  
                    cout << "Opened file to save data to" << endl;
                  
                    // write length information to file
                    //<enter code here>
                    output_file << length << endl << endl;
                  
                    // write data to file
                    //<enter code here>
                    int i;
                    for (i=0;i=length;i++);
                    {
                  	  output_file << OpAmp[i].Name << endl;
                  	  output_file << OpAmp[i].PinCount << endl;
                  	  output_file << OpAmp[i].SlewRate << endl <<endl;
                    }
                  
                    // close the file
                    output_file.close();
                  }
                  Last edited by horace1; Mar 14 '07, 04:45 PM. Reason: added cide tags

                  Comment

                  • ConfusedAlot
                    New Member
                    • Mar 2007
                    • 17

                    #10
                    so i changed the for (i=0;i=length;i ++) to (i=0;i<=length; i++) but the file created does not show the data entered by the user which are to be saved... help!!

                    Comment

                    • Ganon11
                      Recognized Expert Specialist
                      • Oct 2006
                      • 3651

                      #11
                      When you open DATABASE_FILENA ME to store the info, is the file being created? If so, what are its contents after saving? If not, check what DATABASE_FILENA ME contains, and consider exlicitly opening a file such as "DBOutput.d at" or possibly getting the filename from the user.

                      Comment

                      Working...