Advertisement
Guest User

C++

a guest
Mar 30th, 2015
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include<iostream>
  2. #include<fstream>
  3.  
  4. using namespace std;
  5. //Creating a class student
  6. class student
  7. {
  8. private:
  9. int roll_no;
  10. char name[100];
  11. char dept_name[100];
  12. int marks[5];
  13. public:
  14. void get_input();
  15. void calculate();
  16. void print_data();
  17. void filein(int);
  18. void fileout();
  19. int countstudent();
  20. };
  21. //This function is used to read the student records from file Student.dat
  22. void student::filein(int pn)
  23. {
  24. ifstream infile;
  25. infile.open("Student.dat",ios::binary);
  26. infile.seekg(pn*sizeof(student));
  27. infile.read((char*) this,sizeof(*this));
  28. }
  29. //This function is used to write the student records into file Student.dat. If previous data exists new records are appended
  30. void student::fileout()
  31. {
  32. ofstream outfile;
  33. outfile.open("Student.dat",ios::app|ios::binary);
  34. get_input();
  35. outfile.write((char*)this,sizeof(*this));
  36. }
  37. //This function is used to count number of students recorded in the file
  38. int student::countstudent()
  39. {
  40. ifstream infile;
  41. infile.open("Student.dat",ios::binary);
  42. infile.seekg(0,ios::end);
  43. int n=infile.tellg()/sizeof(student);
  44. return n;
  45. }
  46. //This function is used to get all the inputs
  47. void student::get_input()
  48. {
  49. cout<<"\nEnter roll-no:";
  50. cin>>roll_no;
  51. cout<<"\nEnter name:";
  52. getchar();
  53. cin.getline(name,100);
  54. cout<<"\nEnter department name:";
  55. cin.getline(dept_name,100);
  56. }
  57.  
  58. //This function is used to print all the information
  59. void student::print_data()
  60. {
  61. cout<<"\n\nRoll:"<<roll_no;
  62. cout<<"\n\nName:"<<name;
  63. cout<<"\n\nDepartment:"<<dept_name;
  64. }
  65.  
  66.  
  67. int main()
  68. {
  69. student s;
  70. int n;
  71. char ch;
  72. do
  73. {
  74. cout<<"Enter data for student\n-----------------------------------------\n";
  75. s.fileout();//Read from user and write to file
  76. cout<<"Enter another(Y/N):";
  77. cin>>ch;
  78. }while(ch=='Y'||ch=='y');
  79. n=s.countstudent();
  80. for(int i=n-1;i>=0;i--)
  81. {
  82. cout<<"\n\nStudent "<<i+1<<"\n----------------";
  83. s.filein(i);//Read from file
  84. s.print_data();//Print records in reverse order
  85. }
  86. return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement