Help,

I still cannot understand why the following function will not write the change to my program correctly. For my other functions (add a record and delete record, it wrote all the records perfectly to the file, but for some reason when I go to update a record it does not do this. Someone said it could be my seekg but I don't understand why it worked before but not on this function:

// update the new quantity and unit price in the record
void Tools::updateRecord( fstream &updateFile )
 {
    // obtain number of account to update
	 int partNumber = getPart( "\nEnter tool part identification number to update: " );

    // move file-position pointer to correct record in file           
    updateFile.seekg( ( partNumber - 1 ) * sizeof ( Tools ) );

    // read first record from file
    Tools tool;
    updateFile.read( reinterpret_cast < char * >( &tool ),
       sizeof( Tools ) );                              

    // update record
    if ( tool.getPartNumber() != 0 )
    {
       printHeader();  
       outputLine( updateFile, tool ); // display the record

       // request user to specify new quantity
       cout << "\nEnter new current quantity: ";
       int currentQuantity;							// change the quantity
       cin >> currentQuantity;
       tool.setInStock( currentQuantity );			// update record 
     		
       // request user enter a new unit price
       cout << "Enter new unit price for the tool: ";
       double currentPrice; // change the unit price
       cin >> currentPrice;
	   tool.setUnitPrice( currentPrice );			// update record

	   cout << "\nPart # " << partNumber << " has been updated.\n";

	   	// print header in the output prompt screen
		printHeader();

       outputLine( updateFile, tool ); // display the record


       // move file-position pointer to correct record in file           
       updateFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) ); 

	   // write updated record over old record in file               
       updateFile.write( reinterpret_cast < const char * >( &tool ),
          sizeof( Tools ) );


    } // end if
    else // display error if account does not exist
       cerr << "\nPart identification # " << partNumber << " has no information." << endl;
} // end function updateRecord

I also am including the outputLine function that is called:

void Tools::outputLine(ostream &output, const Tools &record )
{
	output << left << setw(10 ) << record.getPartNumber() 
		<< setw( 16 ) << record.getToolName() 
		<< right << setw(9) << record.getInStock()
        << right << setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;

    cout << left << setw(10) << record.getPartNumber() 
		<< setw( 16 ) << record.getToolName() 
		<< right << setw(12) << record.getInStock()
        << right<< setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
}

Please, present Tools class data members declarations. For example, you can't write/read an object with std::string members via low-level binary transput mechanics...

Please, present Tools class data members declarations. For example, you can't write/read an object with std::string members via low-level binary transput mechanics...

Hello,

The following is my code:

class Tools
{
public:
	Tools(int = -1, string = "", int = 0, double = 0.0);	// default tools constructor
	// accessor functions for partNumber, toolName, inStock, unitPrice
	void setPartNumber(int);
	int getPartNumber() const;
	void validatePartNumber(int);

	void setToolName(string);
	string getToolName() const;

	void setInStock(int);
	int getInStock() const;

	void setUnitPrice(double);
	double getUnitPrice() const;

	void createAndInitializeTextFile();
	void enterRecords();
	bool test();
	void printHeader();

	void processChoice();
	int enterChoice();

	void printTextFile(fstream&);
	void updateRecord(fstream&);
	void newRecord( fstream& );
	void deleteRecord( fstream& );
	void outputLine( ostream&, const Tools & );
	int getPart( const char * const );

private:
	enum Choices { PRINT = 1, UPDATE, NEW, DELETE, END };
	int partNumber;			// part id number (tool identifitcation number) is the record number (record key)
	char toolName[20];		// tool name
	int inStock;			// in stock
	double unitPrice;		// price per unit
}; // end class Tools

Well, it's OK to read/write this class objects as binary records. I don't see explicit errors in seekp/write code. This mechanics works perfectly in my test code snippet...

Insert error check code in read/write and seekg/seekp calls. You may use fail(0 or good() stream member function or simplest test conditions, for example:

if (!updateFile.seekp(...))
{
   // seekp failed...
}

May be it helps...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.