Hello,
I need help to try and figure out why one of my functions does not write to the file. I have 4 functions (below) and all of them write to the file except the updateRecord function. I know this is the case because I keep checking the .dat file and also because I keep hitting option 1 which is to print, the printTextFile() function, which prints all the records. When I add and delete a record it works, but for the updateRecord it will update it on the screen but not the actual text file.
printTextFile(fstream&);
updateRecord(fstream&);
newRecord( fstream& );
deleteRecord( fstream& );
Also, I was wondering, once I write to a file shouldn't the file stay in existence even when the program ends?
My functions are as follow:
// create formatted text file for printing
void Tools::printTextFile(fstream &readFromFile )
{
// create text file
ofstream outPrintFile( "print.txt", ios::out );
// exit program if ofstream cannot create file
if ( !outPrintFile )
{
cerr << "File could not be created." << endl;
exit( 1 );
} // end if
// print header in the test file (formatted this way to easily view)
outPrintFile << left << setw(10) << "Tool Id"
<< right << setw(10)<< " Tool Name"
<< right << setw(20) << "Quantity"
<< right << setw(20) << " Unit Price" << endl;
// print header in the output prompt screen
printHeader();
// set file-position pointer to beginning of readFromFile
readFromFile.seekg( 0 );
// read first record from record file
Tools tool;
readFromFile.read( reinterpret_cast < char * >( &tool ),
sizeof ( Tools ) );
// copy all records from record file into text file
while ( !readFromFile.eof() )
{
// write single record to text file
if ( tool.getPartNumber() != 0 ) // skip empty records
outputLine( outPrintFile, tool );
// read next record from record file
readFromFile.read( reinterpret_cast < char * >( &tool),
sizeof ( Tools ) );
} // end while
}
// 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
// create and insert record
void Tools::newRecord( fstream &insertInFile )
{
// obtain number of account to create
int partNumber = getPart( "\nEnter new part number: " );
validatePartNumber(partNumber);
// move file-position pointer to correct record in file
insertInFile.seekg( ( partNumber - 1 ) * sizeof( Tools ) );
// read record from file
Tools tool;
insertInFile.read( reinterpret_cast < char * >( &tool ),
sizeof( Tools ) );
// create record, if record does not previously exist
if ( tool.getPartNumber() == 0 )
{
// user enters tool name, quantity and unit price
cout << "Enter the tool name: ";
fflush(stdin);
cin.getline( toolName, 20, '\n' );
cout << "Enter the quantity in stock: ";
cin >> inStock;
cout << "Enter the unit price: ";
cin >> unitPrice;
// set the record for the part number, tool name, quantity in stock, and unit price
tool.setPartNumber( partNumber );
tool.setToolName( toolName );
tool.setInStock( inStock );
tool.setUnitPrice( unitPrice );
// move file-position pointer to correct record in file
insertInFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) );
// insert record in file
insertInFile.write( reinterpret_cast < const char * >( &tool ),
sizeof( Tools ) );
} // end if
else // display error if account already exists
cerr << "\n\nPart # " << partNumber << " already contains information." << endl;
} // end function newRecord
// delete an existing record
void Tools::deleteRecord( fstream &deleteFromFile )
{
// obtain number of account to delete
int partNumber = getPart( "Enter the tool to delete: " );
// move file-position pointer to correct record in file
deleteFromFile.seekg( ( partNumber - 1 ) * sizeof( Tools ) );
// read record from file
Tools tool;
deleteFromFile.read( reinterpret_cast < char * >( &tool ),
sizeof( Tools ) );
// delete record, if record exists in file
if ( tool.getPartNumber() != 0 )
{
Tools blankTool(0); // create blank record
// move file-position pointer to correct record in file
deleteFromFile.seekp( ( partNumber - 1 ) *
sizeof( Tools ) );
// replace existing record with blank record
deleteFromFile.write(
reinterpret_cast < const char * >( &blankTool ),
sizeof( Tools ) );
cout << "\nPart # " << partNumber << " deleted.\n";
} // end if
else // display error if record does not exist
cerr << "\nPart # " << partNumber << " is empty.\n";
} // end deleteRecord
// display single record
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;
}