text file - stream object issues

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

text file - stream object issues

 
0
  #1
Aug 1st, 2008
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() :
  1. int main()
  2. {
  3. Tools tool;
  4. tool.createAndInitializeTextFile();
  5. tool.enterRecords();
  6.  
  7. if(tool.test())
  8. {
  9. cout << "\nHardware program successful.\n";
  10. tool.processChoice();
  11. }
  12. else
  13. {
  14. cout <<"\nHardware program failed.\n";
  15. }
  16.  
  17. cout << endl;
  18. return 0;
  19. }

My member function definitions:
  1. void Tools::createAndInitializeTextFile()
  2. {
  3. ofstream outTools( "hardware.dat", ios::out | ios::binary );
  4.  
  5. // exit program if ofstream could not open file
  6. if ( !outTools )
  7. {
  8. cerr << "File could not be opened." << endl;
  9. exit( 1 );
  10. }
  11.  
  12. Tools blankTool(0); // constructor zeros out each data member
  13.  
  14. // output 100 blank records to file
  15. for ( int i = 0; i < 100; i++ )
  16. outTools.write( reinterpret_cast < const char * >( &blankTool ), sizeof ( Tools ) );
  17. }
  18.  
  19. void Tools::enterRecords()
  20. {
  21. fstream outTools( "hardware.dat", ios::in | ios::out | ios::binary );
  22.  
  23. // exit program if fstream cannot open file
  24. if ( !outTools )
  25. {
  26. cerr << "File could not be opened." << endl;
  27. exit( 1 );
  28. } // end if
  29.  
  30. // require user to specify a tool identification number
  31. cout << "Enter tool identification number (1 to 100, 0 to end input): ";
  32. cin >> partNumber;
  33.  
  34. Tools tool; // create the object
  35.  
  36. // user enters information, which is copied into file
  37. while (partNumber != 0)
  38. {
  39. validatePartNumber(partNumber);
  40.  
  41. // user enters tool name, quantity and unit price
  42. cout << "Enter the tool name: ";
  43. cin.ignore();
  44. cin.getline(toolName, sizeof(toolName));
  45.  
  46. cout << "Enter the quantity in stock: ";
  47. cin >> inStock;
  48. cout << "Enter the unit price: ";
  49. cin >> unitPrice;
  50.  
  51. // set the record for the part number, tool name, quantity in stock, and unit price
  52. tool.setPartNumber( partNumber );
  53. tool.setToolName( toolName );
  54. tool.setInStock( inStock );
  55. tool.setUnitPrice( unitPrice );
  56.  
  57. // seek position in file of user-specified record
  58. outTools.seekp( ( tool.getPartNumber() - 1 ) * sizeof ( Tools ) );
  59.  
  60. // write user-specified information in file
  61. outTools.write( reinterpret_cast < const char * >( &tool ), sizeof ( Tools) );
  62.  
  63. //enable user to enter another account
  64. cout << "\nEnter tool identification number (1 to 100, 0 to end input): ";
  65. cin >> partNumber;
  66.  
  67. } // end while loop
  68.  
  69. } // end enterRecords function
  70.  
  71.  
  72.  
  73. bool Tools::test()
  74. {
  75. Tools tool;
  76. bool answer = false;
  77.  
  78. ifstream inTools( "hardware.dat", ios::in | ios::binary );
  79.  
  80. // exit program if ifstream cannot open file
  81. if ( !inTools )
  82. {
  83. answer = false;
  84. cerr << "File could not be opened." << endl;
  85. exit( 1 );
  86. } // end if
  87. else
  88. {
  89. answer = true;
  90. }
  91. return answer;
  92. }
  93.  
  94.  
  95. void Tools::processChoice()
  96. {
  97. // open file for reading and writing
  98. fstream inOutTools( "hardware.dat", ios::in | ios::out | ios::binary );
  99.  
  100. // exit program if fstream cannot open file
  101. if ( !inOutTools )
  102. {
  103. cerr << "File could not be opened." << endl;
  104. exit ( 1 );
  105. } // end if
  106.  
  107. int choice; // store user choice
  108.  
  109. // enable user to specify action
  110. while ( ( choice = enterChoice() ) != END )
  111. {
  112. switch ( choice )
  113. {
  114. case PRINT: // create text file from record file
  115. printTextFile( inOutTools );
  116. break;
  117. case UPDATE: // update record
  118. updateRecord( inOutTools );
  119. break;
  120. case NEW: // create record
  121. newRecord( inOutTools );
  122. break;
  123. case DELETE: // delete existing record
  124. deleteRecord( inOutTools );
  125. break;
  126. default: // display error if user does not select valid choice
  127. cerr << "Incorrect choice" << endl;
  128. break;
  129. } // end switch
  130.  
  131. inOutTools.clear(); // reset end-of-file indicator
  132. } // end while
  133. } // end function
  134.  
  135.  
  136. // enable user to input menu choice
  137. int Tools::enterChoice()
  138. {
  139. // display available options
  140. cout << "\nEnter your choice:" << endl
  141. << "1 - store a formatted text file of accounts - called \"print.txt\" for printing." << endl
  142. << "2 - update an account" << endl
  143. << "3 - add a new account" << endl
  144. << "4 - delete an account" << endl
  145. << "5 - end program\n? ";
  146.  
  147. int menuChoice;
  148. cin >> menuChoice; // input menu selection from user
  149. return menuChoice;
  150. }
  151.  
  152. // create formatted text file for printing
  153. void Tools::printTextFile(fstream &readFromFile )
  154. {
  155. // create text file
  156. ofstream outPrintFile( "print.txt", ios::out );
  157.  
  158. // exit program if ofstream cannot create file
  159. if ( !outPrintFile )
  160. {
  161. cerr << "File could not be created." << endl;
  162. exit( 1 );
  163. } // end if
  164.  
  165.  
  166. // print header in the test file (formatted this way to easily view)
  167. outPrintFile << left << setw(9) << "Tool Id"
  168. << right << setw(11)<< " Tool Name"
  169. << right << setw(20) << "Quantity"
  170. << right << setw(15) << " Unit Price" << endl;
  171.  
  172. // print header in the output prompt screen
  173. printHeader();
  174.  
  175. // set file-position pointer to beginning of readFromFile
  176. readFromFile.seekg( 0 );
  177.  
  178. // read first record from record file
  179. Tools tool;
  180. readFromFile.read( reinterpret_cast < char * >( &tool ),
  181. sizeof ( Tools ) );
  182.  
  183. // copy all records from record file into text file
  184. while ( !readFromFile.eof() )
  185. {
  186. // write single record to text file
  187. if ( tool.getPartNumber() != 0 ) // skip empty records
  188. outputLine( outPrintFile, tool );
  189.  
  190. // read next record from record file
  191. readFromFile.read( reinterpret_cast < char * >( &tool),
  192. sizeof ( Tools ) );
  193. } // end while
  194. }
  195.  
  196. // update the new quantity and unit price in the record
  197. void Tools::updateRecord( fstream &updateFile )
  198. {
  199. Tools tool;
  200.  
  201. // obtain number of account to update
  202. partNumber = getPart( "\nEnter tool part identification number to update: " );
  203. tool.setPartNumber( partNumber );
  204.  
  205. // move file-position pointer to correct record in file
  206. updateFile.seekg( ( partNumber - 1 ) * sizeof ( Tools ) );
  207.  
  208. // read first record from file
  209. updateFile.read( reinterpret_cast < char * >( &tool ),
  210. sizeof( Tools ) );
  211.  
  212. // update record
  213. if ( tool.getPartNumber() != 0 )
  214. {
  215. printHeader();
  216. outputLine( updateFile, tool ); // display the record
  217.  
  218. cout << "\nEnter new current quantity: ";
  219. int currentQuantity;
  220. cin >> currentQuantity;
  221. tool.setInStock( currentQuantity );
  222.  
  223. // request user enter a new unit price
  224. cout << "Enter new unit price for the tool: ";
  225. double currentPrice; // change the unit price
  226. cin >> currentPrice;
  227. tool.setUnitPrice( currentPrice );
  228.  
  229. cout << "\nPart # " << partNumber << " has been updated.\n";
  230.  
  231. printHeader();
  232.  
  233. outputLine( updateFile, tool ); // display the record
  234.  
  235. tool.setPartNumber( partNumber );
  236. tool.setToolName( toolName );
  237. tool.setInStock( inStock );
  238. tool.setUnitPrice( unitPrice );
  239.  
  240.  
  241. // move file-position pointer to correct record in file
  242. updateFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) );
  243.  
  244. // insert record in file
  245. updateFile.write( reinterpret_cast < const char * >( &tool ),
  246. sizeof( Tools ) );
  247.  
  248. } // end if
  249. else // display error if account does not exist
  250. cerr << "\nPart identification # " << partNumber << " has no information." << endl;
  251. } // end function updateRecord
  252.  
  253.  
  254. // create and insert record
  255. void Tools::newRecord( fstream &insertInFile )
  256. {
  257. // obtain number of account to create
  258. int partNumber = getPart( "\nEnter new part number: " );
  259. validatePartNumber(partNumber);
  260.  
  261. // move file-position pointer to correct record in file
  262. insertInFile.seekg( ( partNumber - 1 ) * sizeof( Tools ) );
  263.  
  264. // read record from file
  265. Tools tool;
  266. insertInFile.read( reinterpret_cast < char * >( &tool ),
  267. sizeof( Tools ) );
  268.  
  269. // create record, if record does not previously exist
  270. if ( tool.getPartNumber() == 0 )
  271. {
  272. // user enters tool name, quantity and unit price
  273. cout << "Enter the tool name: ";
  274. fflush(stdin);
  275. cin.getline( toolName, size, '\n' );
  276.  
  277. cout << "Enter the quantity in stock: ";
  278. cin >> inStock;
  279. cout << "Enter the unit price: ";
  280. cin >> unitPrice;
  281.  
  282. // set the record for the part number, tool name, quantity in stock, and unit price
  283. tool.setPartNumber( partNumber );
  284. tool.setToolName( toolName );
  285. tool.setInStock( inStock );
  286. tool.setUnitPrice( unitPrice );
  287.  
  288.  
  289. // move file-position pointer to correct record in file
  290. insertInFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) );
  291.  
  292. // insert record in file
  293. insertInFile.write( reinterpret_cast < const char * >( &tool ),
  294. sizeof( Tools ) );
  295. } // end if
  296. else // display error if account already exists
  297. cerr << "\n\nPart # " << partNumber << " already contains information." << endl;
  298. } // end function newRecord
  299.  
  300.  
  301. // delete an existing record
  302. void Tools::deleteRecord( fstream &deleteFromFile )
  303. {
  304. // obtain number of account to delete
  305. int partNumber = getPart( "Enter the tool to delete: " );
  306.  
  307. // move file-position pointer to correct record in file
  308. deleteFromFile.seekg( ( partNumber - 1 ) * sizeof( Tools ) );
  309.  
  310. // read record from file
  311. Tools tool;
  312. deleteFromFile.read( reinterpret_cast < char * >( &tool ),
  313. sizeof( Tools ) );
  314.  
  315. // delete record, if record exists in file
  316. if ( tool.getPartNumber() != 0 )
  317. {
  318. Tools blankTool(0); // create blank record
  319.  
  320. // move file-position pointer to correct record in file
  321. deleteFromFile.seekp( ( partNumber - 1 ) *
  322. sizeof( Tools ) );
  323.  
  324. // replace existing record with blank record
  325. deleteFromFile.write(
  326. reinterpret_cast < const char * >( &blankTool ),
  327. sizeof( Tools ) );
  328.  
  329. cout << "\nPart # " << partNumber << " deleted.\n";
  330. } // end if
  331. else // display error if record does not exist
  332. cerr << "\nPart # " << partNumber << " is empty.\n";
  333. } // end deleteRecord
  334.  
  335. // display single record
  336. void Tools::outputLine(ostream &output, const Tools &record )
  337. {
  338. output << left << setw(11 ) << record.getPartNumber()
  339. << setw(16) << record.getToolName()
  340. << right << setw(9) << record.getInStock()
  341. << right << setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
  342.  
  343. cout << left << setw(10) << record.getPartNumber()
  344. << setw(18) << record.getToolName()
  345. << right << setw(5) << record.getInStock()
  346. << right << setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
  347. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: text file - stream object issues

 
0
  #2
Aug 1st, 2008
Well, it seems you have a problem.
This is not enough: what and where is a problem?..
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

Re: text file - stream object issues

 
0
  #3
Aug 1st, 2008
Originally Posted by ArkM View Post
Well, it seems you have a problem.
This is not enough: what and where is a problem?..
I can't find my hardware.dat file on the computer and also the write in the updateRecord() function does not work.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,519
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: text file - stream object issues

 
0
  #4
Aug 2nd, 2008
>>I can't find my hardware.dat file on the compute
It will be in the same directory that contains the program *.exe file.


>>the write in the updateRecord() function does not work.
what makes you think it doesn't work? What does it do that it's not supposed to do, or what doesn't it do that its supposed to do ? Just by looking at the function it appears to be working right, but since we don't have all the code for the entire program its not possible for us to test it.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 96
Reputation: QuantNeeds is an unknown quantity at this point 
Solved Threads: 0
QuantNeeds QuantNeeds is offline Offline
Junior Poster in Training

Re: text file - stream object issues

 
0
  #5
Aug 2nd, 2008
Originally Posted by Ancient Dragon View Post
>>I can't find my hardware.dat file on the compute
It will be in the same directory that contains the program *.exe file.


>>the write in the updateRecord() function does not work.
what makes you think it doesn't work? What does it do that it's not supposed to do, or what doesn't it do that its supposed to do ? Just by looking at the function it appears to be working right, but since we don't have all the code for the entire program its not possible for us to test it.
Basically I typed in the input and then hit 1 to print the file. I can find the print.dat file but I cannot find the hardware.dat.

Basically I try to make a change and then hit 1 again and when I go back to check the print.dat it shows it in the output screen but then hit 1 again and there is no change, the change also does NOT occur in the print.dat. So when I could not find the hardware.dat this makes me think that I am not recording there to begin with.

All of my code is below:

header:
  1. class Tools
  2. {
  3. public:
  4. Tools(int = -1, string = "", int = 0, double = 0.0); // default tools constructor
  5. // accessor functions for partNumber, toolName, inStock, unitPrice
  6. void setPartNumber(int);
  7. int getPartNumber() const;
  8. void validatePartNumber(int);
  9.  
  10. void setToolName(string);
  11. string getToolName() const;
  12.  
  13. void setInStock(int);
  14. int getInStock() const;
  15.  
  16. void setUnitPrice(double);
  17. double getUnitPrice() const;
  18.  
  19. void createAndInitializeTextFile();
  20. void enterRecords();
  21. bool test();
  22. void printHeader();
  23.  
  24. void processChoice();
  25. int enterChoice();
  26.  
  27. void printTextFile(fstream&);
  28. void updateRecord(fstream&);
  29. void newRecord( fstream& );
  30. void deleteRecord( fstream& );
  31. void outputLine( ostream&, const Tools & );
  32. int getPart( const char * const );
  33.  
  34. private:
  35. static const int size = 20;
  36. enum Choices { PRINT = 1, UPDATE, NEW, DELETE, END };
  37. int partNumber; // part id number (tool identifitcation number) is the record number (record key)
  38. char toolName[20]; // tool name
  39. int inStock; // in stock
  40. double unitPrice; // price per unit
  41. }; // end class Tools

member functions:
  1. // default Tools constructor
  2. Tools::Tools( int partNumberValue,string toolNameValue, int inStockValue, double unitPriceValue)
  3. {
  4. setPartNumber( partNumberValue);
  5. setToolName( toolNameValue );
  6. setInStock( inStockValue);
  7. setUnitPrice( unitPriceValue );
  8. } // end Tools constructor
  9.  
  10.  
  11. // get tool identification number
  12. int Tools::getPartNumber() const
  13. {
  14. return partNumber;
  15. }
  16. // set tool identification numbers
  17. void Tools::setPartNumber( int partNumberValue)
  18. {
  19. partNumber = partNumberValue;
  20. }
  21. void Tools::validatePartNumber(int checkPart)
  22. {
  23. if(checkPart > 0 && checkPart <= 100)
  24. partNumber = checkPart;
  25. else
  26. {
  27. bool flag = false;
  28. while(flag == false)
  29. {
  30. cout << "Part number is a record, an integer within range 1 to 100.";
  31. cout << "\nRe-enter tool identification number (1 to 100, 0 to end input)\n? ";
  32. cin >> checkPart;
  33.  
  34. if(checkPart > 0 && checkPart <= 100)
  35. {
  36. partNumber = checkPart;
  37. flag = true;
  38. }
  39. } // end while loop
  40. } // end if
  41. } // end function
  42.  
  43. // get tool name
  44. string Tools::getToolName() const
  45. {
  46. return toolName;
  47. }
  48. // set the tool name
  49. void Tools::setToolName(string toolNameString)
  50. {
  51. // copy at most 20 characters for the tool name
  52. const char *toolNameValue = toolNameString.data();
  53. int length = int(toolNameString.size());
  54. length = (length < size ? length : 19);
  55. strncpy(toolName, toolNameValue, length);
  56. toolName[ length ] = '\0'; // append null character to lastName
  57. }
  58.  
  59. // get in-stock quantity for the tool
  60. int Tools::getInStock() const
  61. {
  62. return inStock;
  63. }
  64. // set in-stock quantity for the tool
  65. void Tools::setInStock( int inStockValue)
  66. {
  67. inStock= inStockValue;
  68. }
  69.  
  70. // get unit price for each tool
  71. double Tools::getUnitPrice() const
  72. {
  73. return unitPrice;
  74. }
  75. // set unit price for each tool
  76. void Tools::setUnitPrice( double unitPriceValue )
  77. {
  78. unitPrice = unitPriceValue;
  79. }
  80.  
  81. void Tools::printHeader()
  82. {
  83. cout << left << setw(10) << "\nTool Id"
  84. << left << setw(17)<< " Tool Name"
  85. << right << setw(12) << "Quantity"
  86. << right << setw(15) << "Unit Price" << endl;
  87.  
  88. cout << "------- ----------- -------- ----------" << endl;
  89. }
  90.  
  91. void Tools::createAndInitializeTextFile()
  92. {
  93. ofstream outTools( "hardware.dat", ios::out | ios::binary );
  94.  
  95. // exit program if ofstream could not open file
  96. if ( !outTools )
  97. {
  98. cerr << "File could not be opened." << endl;
  99. exit( 1 );
  100. }
  101.  
  102. Tools blankTool(0); // constructor zeros out each data member
  103.  
  104. // output 100 blank records to file
  105. for ( int i = 0; i < 100; i++ )
  106. outTools.write( reinterpret_cast < const char * >( &blankTool ), sizeof ( Tools ) );
  107. }
  108.  
  109. void Tools::enterRecords()
  110. {
  111. fstream outTools( "hardware.dat", ios::in | ios::out | ios::binary );
  112.  
  113. // exit program if fstream cannot open file
  114. if ( !outTools )
  115. {
  116. cerr << "File could not be opened." << endl;
  117. exit( 1 );
  118. } // end if
  119.  
  120. // require user to specify a tool identification number
  121. cout << "Enter tool identification number (1 to 100, 0 to end input): ";
  122. cin >> partNumber;
  123.  
  124. Tools tool; // create the object
  125.  
  126. // user enters information, which is copied into file
  127. while (partNumber != 0)
  128. {
  129. validatePartNumber(partNumber);
  130.  
  131. // user enters tool name, quantity and unit price
  132. cout << "Enter the tool name: ";
  133. cin.ignore();
  134. cin.getline(toolName, sizeof(toolName));
  135.  
  136. cout << "Enter the quantity in stock: ";
  137. cin >> inStock;
  138. cout << "Enter the unit price: ";
  139. cin >> unitPrice;
  140.  
  141. // set the record for the part number, tool name, quantity in stock, and unit price
  142. tool.setPartNumber( partNumber );
  143. tool.setToolName( toolName );
  144. tool.setInStock( inStock );
  145. tool.setUnitPrice( unitPrice );
  146.  
  147. // seek position in file of user-specified record
  148. outTools.seekp( ( tool.getPartNumber() - 1 ) * sizeof ( Tools ) );
  149.  
  150. // write user-specified information in file
  151. outTools.write( reinterpret_cast < const char * >( &tool ), sizeof ( Tools) );
  152.  
  153. //enable user to enter another account
  154. cout << "\nEnter tool identification number (1 to 100, 0 to end input): ";
  155. cin >> partNumber;
  156.  
  157. } // end while loop
  158.  
  159. } // end enterRecords function
  160.  
  161. bool Tools::test()
  162. {
  163. Tools tool;
  164. bool answer = false;
  165.  
  166. ifstream inTools( "hardware.dat", ios::in | ios::binary );
  167.  
  168. // exit program if ifstream cannot open file
  169. if ( !inTools )
  170. {
  171. answer = false;
  172. cerr << "File could not be opened." << endl;
  173. exit( 1 );
  174. } // end if
  175. else
  176. {
  177. answer = true;
  178. }
  179. return answer;
  180. }
  181. void Tools::processChoice()
  182. {
  183. // open file for reading and writing
  184. fstream inOutTools( "hardware.dat", ios::in | ios::out | ios::binary );
  185.  
  186. // exit program if fstream cannot open file
  187. if ( !inOutTools )
  188. {
  189. cerr << "File could not be opened." << endl;
  190. exit ( 1 );
  191. } // end if
  192.  
  193. int choice; // store user choice
  194.  
  195. // enable user to specify action
  196. while ( ( choice = enterChoice() ) != END )
  197. {
  198. switch ( choice )
  199. {
  200. case PRINT: // create text file from record file
  201. printTextFile( inOutTools );
  202. break;
  203. case UPDATE: // update record
  204. updateRecord( inOutTools );
  205. break;
  206. case NEW: // create record
  207. newRecord( inOutTools );
  208. break;
  209. case DELETE: // delete existing record
  210. deleteRecord( inOutTools );
  211. break;
  212. default: // display error if user does not select valid choice
  213. cerr << "Incorrect choice" << endl;
  214. break;
  215. } // end switch
  216.  
  217. inOutTools.clear(); // reset end-of-file indicator
  218. } // end while
  219. }
  220.  
  221. // enable user to input menu choice
  222. int Tools::enterChoice()
  223. {
  224. // display available options
  225. cout << "\nEnter your choice:" << endl
  226. << "1 - store a formatted text file of accounts - called \"print.txt\" for printing." << endl
  227. << "2 - update an account" << endl
  228. << "3 - add a new account" << endl
  229. << "4 - delete an account" << endl
  230. << "5 - end program\n? ";
  231.  
  232. int menuChoice;
  233. cin >> menuChoice; // input menu selection from user
  234. return menuChoice;
  235. }
  236.  
  237. // create formatted text file for printing
  238. void Tools::printTextFile(fstream &readFromFile )
  239. {
  240. // create text file
  241. ofstream outPrintFile( "print.txt", ios::out );
  242.  
  243. // exit program if ofstream cannot create file
  244. if ( !outPrintFile )
  245. {
  246. cerr << "File could not be created." << endl;
  247. exit( 1 );
  248. } // end if
  249.  
  250.  
  251. // print header in the test file (formatted this way to easily view)
  252. outPrintFile << left << setw(9) << "Tool Id"
  253. << right << setw(11)<< " Tool Name"
  254. << right << setw(20) << "Quantity"
  255. << right << setw(15) << " Unit Price" << endl;
  256.  
  257. // print header in the output prompt screen
  258. printHeader();
  259.  
  260. // set file-position pointer to beginning of readFromFile
  261. readFromFile.seekg( 0 );
  262.  
  263. // read first record from record file
  264. Tools tool;
  265. readFromFile.read( reinterpret_cast < char * >( &tool ),
  266. sizeof ( Tools ) );
  267.  
  268. // copy all records from record file into text file
  269. while ( !readFromFile.eof() )
  270. {
  271. // write single record to text file
  272. if ( tool.getPartNumber() != 0 ) // skip empty records
  273. outputLine( outPrintFile, tool );
  274.  
  275. // read next record from record file
  276. readFromFile.read( reinterpret_cast < char * >( &tool),
  277. sizeof ( Tools ) );
  278. } // end while
  279. }
  280.  
  281. // update the new quantity and unit price in the record
  282. void Tools::updateRecord( fstream &updateFile )
  283. {
  284. Tools tool;
  285. // obtain number of account to update
  286. partNumber = getPart( "\nEnter tool part identification number to update: " );
  287. tool.setPartNumber( partNumber );
  288.  
  289. // move file-position pointer to correct record in file
  290. updateFile.seekg( ( partNumber - 1 ) * sizeof ( Tools ) );
  291.  
  292. // read first record from file
  293.  
  294. updateFile.read( reinterpret_cast < char * >( &tool ),
  295. sizeof( Tools ) );
  296.  
  297. // update record
  298. if ( tool.getPartNumber() != 0 )
  299. {
  300. printHeader();
  301. outputLine( updateFile, tool ); // display the record
  302.  
  303. // request user to specify new quantity
  304. cout << "\nEnter new current quantity: ";
  305. int currentQuantity; // change the quantity
  306. cin >> currentQuantity;
  307. tool.setInStock( currentQuantity ); // update record
  308.  
  309. // request user enter a new unit price
  310. cout << "Enter new unit price for the tool: ";
  311. double currentPrice; // change the unit price
  312. cin >> currentPrice;
  313. tool.setUnitPrice( currentPrice ); // update record
  314.  
  315. cout << "\nPart # " << partNumber << " has been updated.\n";
  316.  
  317. // print header in the output prompt screen
  318. printHeader();
  319.  
  320. outputLine( updateFile, tool ); // display the record
  321.  
  322.  
  323.  
  324. // set the record for the part number, tool name, quantity in stock, and unit price
  325. tool.setPartNumber( partNumber );
  326. tool.setToolName( toolName );
  327. tool.setInStock( inStock );
  328. tool.setUnitPrice( unitPrice );
  329.  
  330.  
  331. // move file-position pointer to correct record in file
  332. updateFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) );
  333.  
  334. // insert record in file
  335. updateFile.write( reinterpret_cast < const char * >( &tool ),
  336. sizeof( Tools ) );
  337.  
  338. //// move file-position pointer to correct record in file
  339. //updateFile.seekp( ( tool.getPartNumber() - 1 ) * sizeof ( Tools ) );
  340.  
  341. //// write updated record over old record in file
  342. //updateFile.write( reinterpret_cast < const char * >( &tool ),sizeof ( Tools) );
  343.  
  344. } // end if
  345. else // display error if account does not exist
  346. cerr << "\nPart identification # " << partNumber << " has no information." << endl;
  347. } // end function updateRecord
  348.  
  349.  
  350. // create and insert record
  351. void Tools::newRecord( fstream &insertInFile )
  352. {
  353. // obtain number of account to create
  354. int partNumber = getPart( "\nEnter new part number: " );
  355. validatePartNumber(partNumber);
  356.  
  357. // move file-position pointer to correct record in file
  358. insertInFile.seekg( ( partNumber - 1 ) * sizeof( Tools ) );
  359.  
  360. // read record from file
  361. Tools tool;
  362. insertInFile.read( reinterpret_cast < char * >( &tool ),
  363. sizeof( Tools ) );
  364.  
  365. // create record, if record does not previously exist
  366. if ( tool.getPartNumber() == 0 )
  367. {
  368. // user enters tool name, quantity and unit price
  369. cout << "Enter the tool name: ";
  370. fflush(stdin);
  371. cin.getline( toolName, size, '\n' );
  372.  
  373. cout << "Enter the quantity in stock: ";
  374. cin >> inStock;
  375. cout << "Enter the unit price: ";
  376. cin >> unitPrice;
  377.  
  378. // set the record for the part number, tool name, quantity in stock, and unit price
  379. tool.setPartNumber( partNumber );
  380. tool.setToolName( toolName );
  381. tool.setInStock( inStock );
  382. tool.setUnitPrice( unitPrice );
  383.  
  384.  
  385. // move file-position pointer to correct record in file
  386. insertInFile.seekp( ( partNumber - 1 ) * sizeof( Tools ) );
  387.  
  388. // insert record in file
  389. insertInFile.write( reinterpret_cast < const char * >( &tool ),
  390. sizeof( Tools ) );
  391. } // end if
  392. else // display error if account already exists
  393. cerr << "\n\nPart # " << partNumber << " already contains information." << endl;
  394. } // end function newRecord
  395.  
  396.  
  397. // delete an existing record
  398. void Tools::deleteRecord( fstream &deleteFromFile )
  399. {
  400. // obtain number of account to delete
  401. int partNumber = getPart( "Enter the tool to delete: " );
  402.  
  403. // move file-position pointer to correct record in file
  404. deleteFromFile.seekg( ( partNumber - 1 ) * sizeof( Tools ) );
  405.  
  406. // read record from file
  407. Tools tool;
  408. deleteFromFile.read( reinterpret_cast < char * >( &tool ),
  409. sizeof( Tools ) );
  410.  
  411. // delete record, if record exists in file
  412. if ( tool.getPartNumber() != 0 )
  413. {
  414. Tools blankTool(0); // create blank record
  415.  
  416. // move file-position pointer to correct record in file
  417. deleteFromFile.seekp( ( partNumber - 1 ) *
  418. sizeof( Tools ) );
  419.  
  420. // replace existing record with blank record
  421. deleteFromFile.write(
  422. reinterpret_cast < const char * >( &blankTool ),
  423. sizeof( Tools ) );
  424.  
  425. cout << "\nPart # " << partNumber << " deleted.\n";
  426. } // end if
  427. else // display error if record does not exist
  428. cerr << "\nPart # " << partNumber << " is empty.\n";
  429. } // end deleteRecord
  430.  
  431. // display single record
  432. void Tools::outputLine(ostream &output, const Tools &record )
  433. {
  434. output << left << setw(11 ) << record.getPartNumber()
  435. << setw(16) << record.getToolName()
  436. << right << setw(9) << record.getInStock()
  437. << right << setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
  438.  
  439. cout << left << setw(10) << record.getPartNumber()
  440. << setw(18) << record.getToolName()
  441. << right << setw(5) << record.getInStock()
  442. << right << setw(15) << setprecision(2) << fixed << showpoint << record.getUnitPrice() << endl;
  443. }
  444. // obtain account-number value from user
  445. int Tools::getPart( const char * const prompt )
  446. {
  447. int partNumber;
  448.  
  449. // obtain part identification number value
  450. do
  451. {
  452. cout << prompt << " (1 - 100): ";
  453. cin >> partNumber;
  454. } while ( partNumber < 1 || partNumber > 100 );
  455. return partNumber;
  456. }

main() :
  1. int main()
  2. {
  3. Tools tool;
  4. tool.createAndInitializeTextFile();
  5. tool.enterRecords();
  6.  
  7. if(tool.test())
  8. {
  9. cout << "\nHardware program successful.\n";
  10. tool.processChoice();
  11. }
  12. else
  13. {
  14. cout <<"\nHardware program failed.\n";
  15. }
  16.  
  17. cout << endl;
  18. return 0;
  19. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,519
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1480
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: text file - stream object issues

 
0
  #6
Aug 2nd, 2008
You apparently didn't get a clean compile with the code you posed because it contains several errors. One error is function outputLine() -- One place passes fstream object as the first parameter while another passes ofstream. If I leave it as ostream like you have it then the function has millions of errors. My suggestion is to NOT use ofstream, but use fstream everywhere so that your program is consistent.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC