Hello,
Finally I am down to the last issue with my program: text file writing and reading.
My file has a function whose ofstream object does work and produces a text file for printing called "print".
This function is the printTextFile(fstream &readFromFile ) function.
But initially it is supposed to be creating a file called "hardware"where I store the records, update the records, add a new record, or delete. Well it seems that the fstream objects here are not working because when I search my file for hardware I cannot find it. I am trying to summarize my functions below with their objects and then including my main() and as well the functions and the function definitions.
createAndInitializeTextFile() - ofstream object that writes
enterRecords() - fstream object that I use to seek records with seekp and then also use to write
processChoice() has the fstream object with hardware.dat that is passed the following functions:
void printTextFile(fstream&);
void updateRecord(fstream&);
void newRecord( fstream& );
void deleteRecord( fstream& );
void outputLine( ostream&, const Tools & );
My code in main() :
int main()
{
Tools tool;
tool.createAndInitializeTextFile();
tool.enterRecords();
if(tool.test())
{
cout << "\nHardware program successful.\n";
tool.processChoice();
}
else
{
cout <<"\nHardware program failed.\n";
}
cout << endl;
return 0;
}
My member function definitions:
void Tools::createAndInitializeTextFile()
{
ofstream outTools( "hardware.dat", ios::out | ios::binary );
// exit program if ofstream could not open file
if ( !outTools )
{
cerr << "File could not be opened." << endl;
exit( 1 );
}
Tools blankTool(0); // constructor zeros out each data member
// output 100 blank records to file
for ( int i = 0; i < 100; i++ )
outTools.write( reinterpret_cast < const char * >( &blankTool ), sizeof ( Tools ) );
}
void Tools::enterRecords()
{
fstream outTools( "hardware.dat", ios::in | ios::out | ios::binary );
// exit program if fstream cannot open file
if ( !outTools )
{
cerr << "File could not be opened." << endl;
exit( 1 );
} // end if
// require user to specify a tool identification number
cout << "Enter tool identification number (1 to 100, 0 to end input): ";
cin >> partNumber;
Tools tool; // create the object
// user enters information, which is copied into file
while (partNumber != 0)
{
validatePartNumber(partNumber);
// user enters tool name, quantity and unit price
cout << "Enter the tool name: ";
cin.ignore();
cin.getline(toolName, sizeof(toolName));
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 );
// seek position in file of user-specified record
outTools.seekp( ( tool.getPartNumber() - 1 ) * sizeof ( Tools ) );
// write user-specified information in file
outTools.write( reinterpret_cast < const char * >( &tool ), sizeof ( Tools) );
//enable user to enter another account
cout << "\nEnter tool identification number (1 to 100, 0 to end input): ";
cin >> partNumber;
} // end while loop
} // end enterRecords function
bool Tools::test()
{
Tools tool;
bool answer = false;
ifstream inTools( "hardware.dat", ios::in | ios::binary );
// exit program if ifstream cannot open file
if ( !inTools )
{
answer = false;
cerr << "File could not be opened." << endl;
exit( 1 );
} // end if
else
{
answer = true;
}
return answer;
}
void Tools::processChoice()
{
// open file for reading and writing
fstream inOutTools( "hardware.dat", ios::in | ios::out | ios::binary );
// exit program if fstream cannot open file
if ( !inOutTools )
{
cerr << "File could not be opened." << endl;
exit ( 1 );
} // end if
int choice; // store user choice
// enable user to specify action
while ( ( choice = enterChoice() ) != END )
{
switch ( choice )
{
case PRINT: // create text file from record file
printTextFile( inOutTools );
break;
case UPDATE: // update record
updateRecord( inOutTools );
break;
case NEW: // create record
newRecord( inOutTools );
break;
case DELETE: // delete existing record
deleteRecord( inOutTools );
break;
default: // display error if user does not select valid choice
cerr << "Incorrect choice" << endl;
break;
} // end switch
inOutTools.clear(); // reset end-of-file indicator
} // end while
} // end function
// enable user to input menu choice
int Tools::enterChoice()
{
// display available options
cout << "\nEnter your choice:" << endl
<< "1 - store a formatted text file of accounts - called \"print.txt\" for printing." << endl
<< "2 - update an account" << endl
<< "3 - add a new account" << endl
<< "4 - delete an account" << endl
<< "5 - end program\n? ";
int menuChoice;
cin >> menuChoice; // input menu selection from user
return menuChoice;
}
// 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(9) << "Tool Id"
<< right << setw(11)<< " Tool Name"
<< right << setw(20) << "Quantity"
<< right << setw(15) << " 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 )
{
Tools tool;
// obtain number of account to update
partNumber = getPart( "\nEnter tool part identification number to update: " );
tool.setPartNumber( partNumber );
// move file-position pointer to correct record in file
updateFile.seekg( ( partNumber - 1 ) * sizeof ( Tools ) );
// read first record from file
updateFile.read( reinterpret_cast < char * >( &tool ),
sizeof( Tools ) );
// update record
if ( tool.getPartNumber() != 0 )
{
printHeader();
outputLine( updateFile, tool ); // display the record
cout << "\nEnter new current quantity: ";
int currentQuantity;
cin >> currentQuantity;
tool.setInStock( currentQuantity );
// 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 );
cout << "\nPart # " << partNumber << " has been updated.\n";
printHeader();
outputLine( updateFile, tool ); // display the record
tool.setPartNumber( partNumber );
tool.setToolName( toolName );
tool.setInStock( inStock );
tool.setUnitPrice( unitPrice );
// move file-position pointer to correct record in file
updateFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) );
// insert 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, size, '\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(11 ) << 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(18) << record.getToolName()
<< right << setw(5) << record.getInStock()
<< right << setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
}