Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. //Function declarations
  4.  
  5. int readLine(FILE* spInventory, int *part, int *dept, char *type, float *cost, int *qty);
  6. float calcValue(float cost, int qty, char type, float* value, float* totA, float* totB);
  7. void printLine(FILE* spReport, int *part, int *dept, char *type, float *cost, int *qty );
  8.  
  9. int main(void)
  10. {
  11. // Local Declarations.
  12. FILE* spInventory;
  13. FILE* spReport;
  14.  
  15. float cost;
  16. int qty;
  17. char type;
  18. float value;
  19. float totA;
  20. float totB;
  21. int part;
  22. int dept;
  23.  
  24. // Statements
  25. printf("Processing data\n");
  26. if (!(spInventory = fopen("inventory.txt", "r")))
  27. {
  28. printf("\aError opening file\n");
  29. return 100;
  30. }
  31. if (!(spReport = fopen ("report.txt", "w")))
  32. {
  33. printf("\aError opening grades file\n");
  34. return 102;
  35. } // if open output
  36.  
  37. while (readLine(spReport, &part, &dept, &type, &cost, &qty))
  38. {
  39. calcValue(cost,qty, type, &value, &totA, &totB);
  40. //printLine();
  41. }
  42.  
  43. fclose(spInventory);
  44. fclose(spReport);
  45.  
  46. printf("End data processing");
  47.  
  48. return 0;
  49. } //main
  50.  
  51. int readLine(FILE* spInventory, int *part, int *dept, char *type, float *cost, int *qty)
  52. {
  53. // Local Declarations
  54. int ioResult;
  55.  
  56. ioResult = fscanf(spInventory, "%d %d %c %f %d\n", part, dept, type, cost, qty);
  57.  
  58. if (ioResult == EOF) //end of file, return false - no more to read
  59. return 0;
  60. else if (ioResult != 4) //error, return false - no more to read
  61. {
  62. printf("\aError reading data\n");
  63. return 0;
  64. }
  65. else // if no error and not EOF, return true there's more to read
  66. {
  67. return 1;
  68. }
  69. } // end of readLine
  70.  
  71. float calcValue(float cost, int qty, char type, float* value, float* totA, float* totB)
  72. {
  73. *value = cost * qty;
  74. *totA = *totA + *value;
  75. *totB = *totB + *value;
  76.  
  77. return 0;
  78.  
  79. }
  80.  
  81. void printLine(FILE* spReport, int *part, int *dept, char *type, float *cost, int *qty )
  82. {
  83. fprintf(spReport, "%d %d %c %f %d", part, dept, type, cost, qty);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement