File Processing Creation/Display Error

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2008
Posts: 9
Reputation: dampecram is an unknown quantity at this point 
Solved Threads: 0
dampecram dampecram is offline Offline
Newbie Poster

File Processing Creation/Display Error

 
0
  #1
Nov 25th, 2008
Hello, the program I have creates a database and lets you update/add/delete any information you want about hardware items. My problem is.. after I exit the program, all the data is lost in the binary file. I need to keep a database in a binary file and update it everytime the user runs the program. I'm confused on how to go about that. example: I run the program and add a item called jigsaw, then exit.
The next time I run the program, I should have a jigsaw in my database.

My other problem is. I already designed the program to send the information to a text file, is there anyway to simply change that and just have the information stored in the file displayed in the program, not in a text file?

here is my program as of right now with information sending to text file and other problem
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include<string.h>
  4.  
  5. struct hardwareData{
  6. int RecordNum;
  7. char ToolName[ 20 ];
  8. int quantity;
  9. double cost;
  10. };
  11.  
  12. int enterChoice( void );
  13. void textFile( FILE *readPtr );
  14. void updateRecord( FILE *fPtr );
  15. void newRecord( FILE *fPtr );
  16. void deleteRecord( FILE *fPtr );
  17. int main( void )
  18. {
  19. FILE *cfPtr;
  20. int choice;
  21.  
  22. if ( ( cfPtr = fopen( "hardware.dat", "wb+" ) ) == NULL ) {
  23. printf( "File could not be opened. \n" );
  24. }
  25. else {
  26.  
  27. while ( ( choice = enterChoice() ) != 5 ) {
  28.  
  29. switch ( choice ) {
  30. case 1:
  31. textFile( cfPtr );
  32. break;
  33.  
  34. case 2:
  35. updateRecord( cfPtr );
  36. break;
  37.  
  38. case 3:
  39. newRecord( cfPtr );
  40. break;
  41.  
  42. case 4:
  43. deleteRecord( cfPtr );
  44. break;
  45.  
  46. default:
  47. printf( "Incorrect choice \n" );
  48. break;
  49. }
  50. }
  51. fclose( cfPtr );
  52. }
  53. system("pause");
  54. return 0;
  55. }
  56.  
  57. void textFile( FILE *readPtr )
  58. {
  59. FILE *writePtr;
  60. struct hardwareData hardware = { 0, "", 0, 0.0 };
  61.  
  62. if ( ( writePtr = fopen( "hardware.list.txt", "w" ) ) == NULL ) {
  63. printf( "File could not be opened. \n" );
  64. }
  65. else {
  66. rewind( readPtr );
  67. fprintf( writePtr, "%-12s%-20s%-11s%10s\n",
  68. "Record #", "Tool Name", "Quantity","Cost" );
  69.  
  70. while ( fread( &hardware, sizeof( struct hardwareData ), 1, readPtr ) == 1 ){
  71.  
  72. if (hardware.RecordNum != 0){
  73. fprintf( writePtr, "%-12d%-20s%-11d%10.2f\n", hardware.RecordNum, hardware.ToolName, hardware.quantity, hardware.cost );
  74. }
  75. }
  76. }
  77. fclose( writePtr );
  78. }
  79. void updateRecord( FILE *fPtr )
  80. {
  81. int record;
  82. int newquantity = 0;
  83. double newcost = 0;
  84. char NewToolName[20];
  85. struct hardwareData hardware = { 0, "", 0, 0.0 };
  86.  
  87. printf( "Enter record to update ( 1 - 100 ): " );
  88. scanf( "%d", &record );
  89.  
  90. fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ),
  91. SEEK_SET );
  92. fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
  93.  
  94. if ( hardware.RecordNum == 0 ) {
  95. printf( "Record #%d has no information. \n", record );
  96. }
  97. else {
  98. printf( "%-12s%-20s%-11s%10s\n",
  99. "Record #", "Tool Name", "Quantity","Cost" );
  100. printf( "%-12d%-20s%-11d%10.2f\n\n", hardware.RecordNum, hardware.ToolName, hardware.quantity, hardware.cost );
  101.  
  102. printf( "Enter new tool name (Without Spaces), Quantity, and Cost: " );
  103. scanf( "%s%d%lf", &NewToolName, &newquantity, &newcost );
  104. hardware.quantity = newquantity;
  105. hardware.cost = newcost;
  106.  
  107. strcpy(hardware.ToolName, NewToolName);
  108. printf( "%-12s%-20s%-11s%10s\n", "Record #", "Tool Name", "Quantity", "Cost" );
  109. printf( "%-12d%-20s%-11d%10.2f\n", hardware.RecordNum, hardware.ToolName, hardware.quantity, hardware.cost );
  110.  
  111. fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
  112. fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
  113. }
  114. }
  115.  
  116. /* delete an existing record */
  117. void deleteRecord( FILE *fPtr )
  118. {
  119. struct hardwareData hardware;
  120. struct hardwareData blankHardware = { 0, " ", 0, 0 };
  121. int RecordNum;
  122.  
  123. printf( "Enter record number to delete ( 1 - 100 ): " );
  124. scanf( "%d", &RecordNum );
  125.  
  126. fseek( fPtr, ( RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
  127. fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
  128.  
  129. if ( hardware.RecordNum == 0 ) {
  130. printf( "Record %d does not exist.\n", RecordNum );
  131. }
  132. else {
  133. fseek( fPtr, ( RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
  134. fwrite( &blankHardware, sizeof( struct hardwareData ), 1, fPtr );
  135. }
  136. }
  137. /* create new record */
  138. void newRecord( FILE *fPtr )
  139. {
  140. struct hardwareData hardware = { 0, "", 0, 0.0 };
  141. int RecordNum;
  142. printf( "Enter new record number ( 1 - 100 ): " );
  143. scanf( "%d", &RecordNum );
  144. fseek( fPtr, ( RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
  145. fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
  146.  
  147. if ( hardware.RecordNum != 0 ) {
  148. printf( "Account #%d already contains information.\n",
  149. hardware.RecordNum );
  150. }
  151. else {
  152. printf( "Enter tool name (Without Spaces), quantity, cost\n? " );
  153. scanf( "%s%d%lf", &hardware.ToolName, &hardware.quantity, &hardware.cost );
  154. hardware.RecordNum = RecordNum;
  155.  
  156. fseek( fPtr, ( hardware.RecordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
  157. fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
  158. }
  159. }
  160. int enterChoice( void )
  161. {
  162. int menuChoice;
  163. printf( "\nEnter your choice\n"
  164. "1 - Store a formatted text file of acounts called\n"
  165. " \"hardware.list.txt\" for printing\n"
  166. "2 - Update a record\n"
  167. "3 - Add a new record\n"
  168. "4 - Delete a record\n"
  169. "5 - End program\n? " );
  170. scanf( "%d", &menuChoice );
  171. return menuChoice;
  172.  
  173. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,437
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: 1473
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: File Processing Creation/Display Error

 
0
  #2
Nov 25th, 2008
The problem is the way the program opens the file on line 22 of the code you posted. "wb+" will delete the contents of the file if the file already exists. Read about the open flags here.
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