Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Karl Ramberg
- 2 // 2-17-19
- 3 // CS181 - Assignment 1 - Q4
- 4
- 5 #include<iostream>
- 6 #include<fstream>
- 7
- 8 using namespace std;
- 9
- 10 struct Division
- 11 {
- 12 string name;
- 13 int id;
- 14 double sales;
- 15 int year;
- 16 };
- 17
- 18 void openFile(string &filename, fstream &file);
- 19
- 20 void readFile(string filename, fstream &file);
- 21
- 22 void writeFile(string filename, fstream &file);
- 23
- 24 void modifyFile(string filename, fstream &file);
- 25
- 26 int main()
- 27 {
- 28 fstream file;
- 29 string filename;
- 30 openFile(filename, file);
- 31
- 32 int choice;
- 33 cout << "(1) Read, (2) Write, or (3) Modify? ";
- 34 cin >> choice;
- 35
- 36 while(choice != 1 && choice != 2 && choice != 3)
- 37 {
- 38 cout << "Not a valid operation" << endl;
- 39 cout << "(1) Read, (2) Write, or (3) Modify? ";
- 40 cin >> choice;
- 41 }
- 42
- 43 if(choice == 1)
- 44 {
- 45 readFile(filename, file);
- 46 }
- 47 else if(choice == 2)
- 48 {
- 49 writeFile(filename, file);
- 50 }
- 51 else
- 52 {
- 53 modifyFile(filename, file);
- 54 }
- 55 }
- 56
- 57 void openFile(string &filename, fstream &file)
- 58 {
- 59 cout << "Choose a file: ";
- 60 cin >> filename;
- 61
- 62 file.open(filename, ios::in | ios::out | ios::binary);
- 11 {
- 12 string name;
- 13 int id;
- 14 double sales;
- 15 int year;
- 16 };
- 17
- 18 void openFile(string &filename, fstream &file);
- 19
- 20 void readFile(string filename, fstream &file);
- 21
- 22 void writeFile(string filename, fstream &file);
- 23
- 24 void modifyFile(string filename, fstream &file);
- 25
- 26 int main()
- 27 {
- 28 fstream file;
- 29 string filename;
- 30 openFile(filename, file);
- 31
- 32 int choice;
- 33 cout << "(1) Read, (2) Write, or (3) Modify? ";
- 34 cin >> choice;
- 35
- 36 while(choice != 1 && choice != 2 && choice != 3)
- 37 {
- 38 cout << "Not a valid operation" << endl;
- 39 cout << "(1) Read, (2) Write, or (3) Modify? ";
- 40 cin >> choice;
- 41 }
- 42
- 43 if(choice == 1)
- 44 {
- 45 readFile(filename, file);
- 46 }
- 47 else if(choice == 2)
- 48 {
- 49 writeFile(filename, file);
- 50 }
- 51 else
- 52 {
- 53 modifyFile(filename, file);
- 54 }
- 55
- 56 file.close();
- 57 }
- 58
- 59 void openFile(string &filename, fstream &file)
- 60 {
- 61 cout << "Choose a file: ";
- 62 cin >> filename;
- 63
- 64 file.open(filename, ios::in | ios::out | ios::binary);
- 65 while(file.fail())
- 66 {
- 67 cout << "Cannot read that file" << endl;
- 68 cout << "Choose a file: ";
- 69 cin >> filename;
- 70 file.open(filename, ios::in | ios::out | ios::binary);
- 71 }
- 72 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement