Advertisement
NoHatred0

Untitled

Jan 27th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.34 KB | None | 0 0
  1. /**
  2. * This program reads a random access file sequentially,
  3. * updates data already written to the file, creates new
  4. * data to be placed in the file, and deletes data
  5. * already in the file.
  6. */
  7. #include <stdio.h>
  8. struct clientData {
  9. int acctNum;
  10. char lastName[ 15 ];
  11. char firstName[ 10 ];
  12. double balance;
  13. };
  14. int enterChoice( void );
  15. void textFile( FILE * );
  16. void updateRecord( FILE * );
  17. void newRecord( FILE * );
  18. void deleteRecord( FILE * );
  19.  
  20. int main()
  21. {
  22. FILE *cfPtr;
  23. int choice;
  24. if ( ( cfPtr = fopen( "credit.dat", "r+b" ) ) == NULL ) printf( "File could not be opened.\n" );
  25. else {
  26. while ( ( choice = enterChoice() ) != 5 ) {
  27. switch ( choice ) {
  28. case 1: textFile( cfPtr ); break;
  29. case 2: updateRecord( cfPtr );break;
  30. case 3: newRecord( cfPtr );break;
  31. case 4: deleteRecord( cfPtr );break;
  32. }
  33. }
  34. fclose( cfPtr );
  35. }
  36. return 0;
  37. }
  38. void textFile( FILE *readPtr )
  39. {
  40. FILE *writePtr;
  41. struct clientData client = { 0, "", "", 0.0 };
  42. if ( ( writePtr = fopen( "accounts.txt", "wt" ) ) == NULL ) printf( "File could not be opened.\n" );
  43. else {
  44. rewind( readPtr );
  45. fprintf( writePtr, "%-6s%-16s%-11s%10s\n", "Acct", "Last Name", "First Name","Balance" );
  46. while ( !feof( readPtr ) ) {
  47. fread( &client, sizeof( struct clientData ), 1, readPtr );
  48. if ( client.acctNum != 0 )
  49. fprintf( writePtr, "%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName, client.firstName, client.balance );
  50. }
  51. fclose( writePtr );
  52. }
  53. }
  54.  
  55. void updateRecord( FILE *fPtr )
  56. {
  57. int account;
  58. double transaction;
  59. struct clientData client = { 0, "", "", 0.0 };
  60.  
  61. printf( "Enter account to update ( 1 - 100 ): " );
  62. scanf( "%d", &account );
  63. fseek( fPtr, ( account - 1 ) * sizeof( struct clientData ), SEEK_SET );
  64. fread( &client, sizeof( struct clientData ), 1, fPtr );
  65. if ( client.acctNum == 0 )
  66. printf( "Acount #%d has no information.\n", account );
  67. else {
  68. printf( "%-6d%-16s%-11s%10.2f\n\n",client.acctNum, client.lastName,client.firstName, client.balance );
  69. printf( "Enter charge ( + ) or payment ( - ): " );
  70. scanf( "%lf", &transaction );
  71. client.balance += transaction;
  72. printf( "%-6d%-16s%-11s%10.2f\n",client.acctNum, client.lastName,client.firstName, client.balance );
  73. fseek( fPtr,( account - 1 ) * sizeof( struct clientData ),SEEK_SET );
  74. fwrite( &client, sizeof( struct clientData ), 1,fPtr );
  75. }
  76. }
  77.  
  78. void deleteRecord( FILE *fPtr )
  79. {
  80. struct clientData client,
  81. blankClient = { 0, "", "", 0 };
  82. int accountNum;
  83. printf( "Enter account number to delete ( 1 - 100 ): " );
  84. scanf( "%d", &accountNum );
  85. fseek( fPtr, ( accountNum - 1 ) * sizeof( struct clientData ), SEEK_SET );
  86. fread( &client, sizeof( struct clientData ), 1, fPtr );
  87. if ( client.acctNum == 0 )
  88. printf( "Account %d does not exist.\n", accountNum );
  89. else {
  90. fseek( fPtr, ( accountNum - 1 ) * sizeof( struct clientData ), SEEK_SET );
  91. fwrite( &blankClient, sizeof( struct clientData ), 1, fPtr );
  92. }
  93. }
  94.  
  95. void newRecord( FILE *fPtr )
  96. {
  97. struct clientData client = { 0, "", "", 0.0 };
  98. int accountNum;
  99.  
  100. printf( "Enter new account number ( 1 - 100 ): " );
  101. scanf( "%d", &accountNum );
  102.  
  103. fseek( fPtr,( accountNum - 1 ) * sizeof( struct clientData ),SEEK_SET );
  104.  
  105. fread( &client, sizeof( struct clientData ), 1, fPtr );
  106.  
  107. if ( client.acctNum != 0 )
  108. printf( "Account #%d already contains information.\n", client.acctNum );
  109. else {
  110. printf( "Enter lastname, firstname, balance\n? " );
  111. scanf( "%s%s%lf", &client.lastName, &client.firstName, &client.balance );
  112. client.acctNum = accountNum;
  113.  
  114. fseek( fPtr, ( client.acctNum - 1 ) * sizeof( struct clientData ), SEEK_SET );
  115. fwrite( &client, sizeof( struct clientData ), 1, fPtr );
  116. }
  117. }
  118. int enterChoice( void )
  119. {
  120. int menuChoice;
  121. printf( "\nEnter your choice\n 1 - store a formatted text file of acounts called\n \"accounts.txt\" for printing\n 2 - update an account\n3 - add a new account\n4 - delete an account\n5 - end program\n? " );
  122. scanf( "%d", &menuChoice );
  123. return menuChoice;
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement