Advertisement
Guest User

Untitled

a guest
Feb 17th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. // Karl Ramberg
  2. 2 // 2-17-19
  3. 3 // CS181 - Assignment 1 - Q4
  4. 4
  5. 5 #include<iostream>
  6. 6 #include<fstream>
  7. 7
  8. 8 using namespace std;
  9. 9
  10. 10 struct Division
  11. 11 {
  12. 12 string name;
  13. 13 int id;
  14. 14 double sales;
  15. 15 int year;
  16. 16 };
  17. 17
  18. 18 void openFile(string &filename, fstream &file);
  19. 19
  20. 20 void readFile(string filename, fstream &file);
  21. 21
  22. 22 void writeFile(string filename, fstream &file);
  23. 23
  24. 24 void modifyFile(string filename, fstream &file);
  25. 25
  26. 26 int main()
  27. 27 {
  28. 28 fstream file;
  29. 29 string filename;
  30. 30 openFile(filename, file);
  31. 31
  32. 32 int choice;
  33. 33 cout << "(1) Read, (2) Write, or (3) Modify? ";
  34. 34 cin >> choice;
  35. 35
  36. 36 while(choice != 1 && choice != 2 && choice != 3)
  37. 37 {
  38. 38 cout << "Not a valid operation" << endl;
  39. 39 cout << "(1) Read, (2) Write, or (3) Modify? ";
  40. 40 cin >> choice;
  41. 41 }
  42. 42
  43. 43 if(choice == 1)
  44. 44 {
  45. 45 readFile(filename, file);
  46. 46 }
  47. 47 else if(choice == 2)
  48. 48 {
  49. 49 writeFile(filename, file);
  50. 50 }
  51. 51 else
  52. 52 {
  53. 53 modifyFile(filename, file);
  54. 54 }
  55. 55 }
  56. 56
  57. 57 void openFile(string &filename, fstream &file)
  58. 58 {
  59. 59 cout << "Choose a file: ";
  60. 60 cin >> filename;
  61. 61
  62. 62 file.open(filename, ios::in | ios::out | ios::binary);
  63. 11 {
  64. 12 string name;
  65. 13 int id;
  66. 14 double sales;
  67. 15 int year;
  68. 16 };
  69. 17
  70. 18 void openFile(string &filename, fstream &file);
  71. 19
  72. 20 void readFile(string filename, fstream &file);
  73. 21
  74. 22 void writeFile(string filename, fstream &file);
  75. 23
  76. 24 void modifyFile(string filename, fstream &file);
  77. 25
  78. 26 int main()
  79. 27 {
  80. 28 fstream file;
  81. 29 string filename;
  82. 30 openFile(filename, file);
  83. 31
  84. 32 int choice;
  85. 33 cout << "(1) Read, (2) Write, or (3) Modify? ";
  86. 34 cin >> choice;
  87. 35
  88. 36 while(choice != 1 && choice != 2 && choice != 3)
  89. 37 {
  90. 38 cout << "Not a valid operation" << endl;
  91. 39 cout << "(1) Read, (2) Write, or (3) Modify? ";
  92. 40 cin >> choice;
  93. 41 }
  94. 42
  95. 43 if(choice == 1)
  96. 44 {
  97. 45 readFile(filename, file);
  98. 46 }
  99. 47 else if(choice == 2)
  100. 48 {
  101. 49 writeFile(filename, file);
  102. 50 }
  103. 51 else
  104. 52 {
  105. 53 modifyFile(filename, file);
  106. 54 }
  107. 55
  108. 56 file.close();
  109. 57 }
  110. 58
  111. 59 void openFile(string &filename, fstream &file)
  112. 60 {
  113. 61 cout << "Choose a file: ";
  114. 62 cin >> filename;
  115. 63
  116. 64 file.open(filename, ios::in | ios::out | ios::binary);
  117. 65 while(file.fail())
  118. 66 {
  119. 67 cout << "Cannot read that file" << endl;
  120. 68 cout << "Choose a file: ";
  121. 69 cin >> filename;
  122. 70 file.open(filename, ios::in | ios::out | ios::binary);
  123. 71 }
  124. 72 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement