Created
June 10, 2020 15:18
-
-
Save lablnet/b1deb6c6351b6db42d2e7728931a8af2 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<iostream> | |
| #include<fstream> | |
| using namespace std; | |
| // define a structure to store student data | |
| struct student { | |
| int sapid; | |
| char name[30]; | |
| float marks; | |
| void getData(); // get student data from user | |
| void displayData(); // display data | |
| }; | |
| void student::getData() { | |
| cout << "\nEnter SAPId. : "; | |
| cin >> sapid; | |
| cin.ignore(); // ignore the newline char inserted when you press enter | |
| cout << "Enter Name : "; | |
| cin.getline(name, 30); | |
| cout << "Enter Marks : "; | |
| cin >> marks; | |
| } | |
| void student::displayData() { | |
| cout << "SAP Id. : " << sapid << endl; | |
| cout << "Name : " << name << endl; | |
| cout << "Marks : " << marks << endl; | |
| } | |
| void menu(); | |
| void writeData(); | |
| void readData(); | |
| void displayData(); | |
| void deleteData(); | |
| int maxSize = 3; | |
| int no_of_records = 0; | |
| student s[3]; // array of 3 students | |
| ofstream wfile; | |
| ifstream rfile; | |
| //to count to number of students after deletion | |
| int main() { | |
| int choice; | |
| while (true) | |
| { | |
| menu(); | |
| cin >> choice; | |
| switch (choice) | |
| { | |
| case 1: | |
| writeData(); | |
| break; | |
| case 2: | |
| readData(); | |
| break; | |
| case 3: | |
| displayData(); | |
| break; | |
| case 4: | |
| deleteData(); | |
| break; | |
| case 5: | |
| exit(0); | |
| default: | |
| break; | |
| }//end switch() | |
| }//end while() | |
| return 0; | |
| }//end main() | |
| void menu() | |
| { | |
| cout << "Press 1 to write to the file" << endl; | |
| cout << "Press 2 to read from file" << endl; | |
| cout << "Press 3 to dispaly student data" << endl; | |
| cout << "Press 4 to delete a student data" << endl; | |
| cout << "Press 5 to exit" << endl; | |
| } | |
| void writeData() | |
| { | |
| wfile.open("students.txt", ios::app); // open file for writing | |
| for (int i = 0; i < maxSize; i++) | |
| { | |
| s[i].getData(); | |
| wfile.write((char *)&s[i], sizeof(s[i])); //(char*)&s - type casting &s into a char pointer. | |
| //(char *)&s[i] Pointer to an array of at least n characters. | |
| } | |
| wfile.close(); // close the file | |
| } | |
| void readData() | |
| { | |
| no_of_records = 0; | |
| rfile.open("students.txt");// , ios::in); // open file for reading | |
| for (int i = 0; i < maxSize; i++) | |
| { | |
| //(char *)&s[i] Pointer to an array where the extracted | |
| //characters are stored. | |
| rfile.read((char *)&s[i], sizeof(s[i])); // read an object from a file | |
| no_of_records++; | |
| } | |
| rfile.close(); // close the file | |
| } | |
| void displayData() | |
| { | |
| for (int i = 0; i < no_of_records; i++) { | |
| s[i].displayData(); | |
| } | |
| } | |
| void deleteData() | |
| { | |
| int sapid, count = 0; | |
| wfile.open("students.txt"); | |
| cout << "enter student SAPId to delete it:"; | |
| cin >> sapid; | |
| for (int i = 0; i < maxSize; i++) | |
| { | |
| if (s[i].sapid != sapid) | |
| { | |
| wfile.write((char *)&s[i], sizeof(s[i])); | |
| count++; | |
| } | |
| } | |
| wfile.close(); | |
| maxSize = count; | |
| readData(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment