Advertisement
desislava_topuzakova

example_files_student

Mar 12th, 2018
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.41 KB | None | 0 0
  1. #include "iostream"
  2. #include "iomanip"
  3. #include "fstream"
  4. #include "string"
  5. #include "ctype.h"
  6. #include "conio.h"
  7.  
  8. using namespace std;
  9. const unsigned MAX=3;
  10.  
  11. char *filename = "student.dat";  //��� �� �����
  12. struct student //��������� �� �������
  13. {
  14.     string facNo; //���. �
  15.     string Name;  //��� �� ��������
  16.     double mark[MAX]; //������ �� ��� ��������
  17. };
  18.  
  19. //������� �� ����������� ����� �� 1 �������
  20. student set_value(string aFacNo, string aName, double m0, double m1, double m2)
  21. {
  22.     student stud;
  23.     stud.facNo = aFacNo;
  24.     stud.Name = aName;
  25.     stud.mark[0] = m0;
  26.     stud.mark[1] = m1;
  27.     stud.mark[2] = m2;
  28.     return stud;
  29. }
  30.  
  31. //���� �� ��������� �������
  32. char Display_menu()
  33. {cout<<"\n\tMenu\nC - Create file\nW - Write to file\nR - Read from file"
  34.     <<"\nA - Append a redor\nU - Updete a redord\nD - Delete a record"
  35.     <<"\nF - Find data\nE - Exit program\n    Youre choice: ";
  36.  char choice;
  37.  choice = _getch();
  38.  choice =toupper(choice);
  39.  return choice;
  40. }
  41.  
  42. //������� �� ��������� �� ������� �� 1 �������
  43. void Input(student &stud)
  44. {
  45.     cout<<"\nfacNo: "; getline(cin,stud.facNo,'\n');
  46.     cout<<"name: "; getline(cin,stud.Name,'\n');
  47.     for(int i = 0; i<MAX; i++)
  48.         {
  49.             cout<<"mark["<<i+1<<"] = ";
  50.             cin>>stud.mark[i];
  51.         }
  52. }
  53.  
  54. //������� �� ����������� ������� �� 1 �������
  55. void Output(student stud)
  56. {
  57.     cout<<setw(10)<<setiosflags(ios::left)<<stud.facNo
  58.         <<setw(25)<<setiosflags(ios::left)<<stud.Name;
  59.     cout<<resetiosflags(ios::left);
  60.     for(int i=0; i<MAX; i++)
  61.         cout<<setw(6)<<setprecision(2)<<setiosflags(ios::fixed)
  62.             <<stud.mark[i];
  63.     cout<<endl;
  64. }
  65.  
  66. //������� �� ��������� �� ������ ����
  67. void Create_file(char *fname)
  68. {
  69.     fstream students;
  70.     student stud;
  71.     students.open(fname,ios::in);
  72.     if(!students) //������ �� ���� �� ���� �������
  73.         {
  74.             cerr<<"File couldn't be opened\n";
  75.             cout<<"Create a new file...\n";
  76.             students.open(fname,ios::out); //������ ��� ����
  77.             students.close();
  78.             return;
  79.         }
  80.     students.seekg(0,ios::end); //���������� get ��������� � ���� �� �����
  81.     streamoff size_file;
  82.     size_file = students.tellg(); //�������� ������� �� �����
  83.     cout<<"file isn't empty. It has "
  84.         <<size_file/sizeof(student)<<" records.\n";
  85.     char Answer;
  86.     cout<<"Delete file? (Y/N): ";
  87.         Answer = _getch();Answer = toupper(Answer);
  88.     if(Answer != 'Y')
  89.             {
  90.                 students.close();
  91.                 return;
  92.             }
  93.     students.clear();
  94.     students.seekg(0);
  95.     students.close();
  96.     students.open(fname,ios::out|ios::trunc);
  97.     students.close();
  98.    
  99. }
  100. //������� �� ��������� �� ����, ���� �������� ������ ������������ � ����.
  101. void Write_to_file(char *fname)
  102. {
  103.     ofstream students;
  104.     student stud;
  105.     students.open(fname,ios::out);
  106.     long i, count;
  107.     cout<<"Enter count of students = ";
  108.     cin>>count;
  109.     for(i=0;i<count; i++)
  110.         {
  111.             cin.ignore();
  112.             Input(stud);
  113.             students.write(reinterpret_cast<char *> (&stud),sizeof(student));
  114.         }
  115.     students.close();
  116. }
  117.  
  118. //������� �� ������ �� ������� �� ���� � ��������� �� �� ������
  119. void Read_from_file(char *filename)
  120. {
  121.     ifstream students;
  122.     student s;
  123.     students.open(filename, ios::in);
  124.     students.clear();
  125.     students.seekg(0);
  126.     while(students.read(reinterpret_cast<char*>(&s),sizeof(student)))
  127.         {
  128.             Output(s);
  129.             if(students.eof()) break;
  130.         }
  131.     students.close();
  132. }
  133.  
  134. //������� ���������� �� ����� � ���� �� �����
  135. void Append_to_file(char *filename)
  136. {
  137.     ofstream students;
  138.     student stud;
  139.     int i,count;
  140.     students.open (filename,ios::app);
  141.     students.seekp(0,ios::end);
  142.     cout<<"Enter count of students = ";
  143.     cin>>count;
  144.     for(i=0;i<count; i++)
  145.         {
  146.             cin.ignore();
  147.             Input(stud);
  148.             students.write(reinterpret_cast<char *> (&stud),sizeof(student));
  149.         }
  150.     students.close();  
  151. }
  152.  
  153. //������� �� ���������� �� ���� ����� ��� �����
  154. void Update_record(char *filename)
  155. {
  156.     fstream students;
  157.     student stud;
  158.     students.open(filename, ios::in);
  159.     students.clear();
  160.     students.seekg(0);
  161.     streamoff record;
  162.     cout<<"Enter record "; cin>>record;
  163.     students.seekg((record-1)*sizeof(student));
  164.     students.read(reinterpret_cast<char*>(&stud),sizeof(student));
  165.     Output(stud);
  166.     char Answer;
  167.     cout<<"Update record? (Y/N): ";
  168.         Answer = _getch();Answer = toupper(Answer);
  169.     if(Answer != 'Y')
  170.             {
  171.                 students.close();
  172.                 return;
  173.             }
  174.     students.close();
  175.     cin.ignore();
  176.     Input(stud);
  177.     students.open(filename, ios::ate| ios::out |ios::_Nocreate);
  178.     students.clear();
  179.     students.seekp((record-1)*sizeof(student));
  180.     students.write(reinterpret_cast<char *> (&stud),sizeof(student));
  181.     students.seekp(0,ios::end);
  182.     students.close();
  183. }
  184.  
  185. //������� �� ��������� �� ���� ����� ��� ����� - �������� ������ � ������ �����
  186. void Dlete_record(char *filename)
  187. {
  188.     fstream students;
  189.     student stud;
  190.     students.open(filename, ios::in);
  191.     students.clear();
  192.     students.seekg(0);
  193.     streamoff record;
  194.     cout<<"Enter record "; cin>>record;
  195.     students.seekg((record-1)*sizeof(student));
  196.     students.read(reinterpret_cast<char*>(&stud),sizeof(student));
  197.     Output(stud);
  198.     char Answer;
  199.     cout<<"Delete record? (Y/N): ";
  200.         Answer = _getch();Answer = toupper(Answer);
  201.     if(Answer != 'Y')
  202.             {
  203.                 students.close();
  204.                 return;
  205.             }
  206.     students.close();
  207.     cin.ignore();
  208.     stud =set_value("no FacNo","No Name",0,0,0);
  209.     students.open(filename, ios::ate| ios::out |ios::_Nocreate);
  210.     students.clear();
  211.     students.seekp((record-1)*sizeof(student));
  212.     students.write(reinterpret_cast<char *> (&stud),sizeof(student));
  213.     students.seekp(0,ios::end);
  214.     students.close();
  215.  
  216.    
  217. }
  218.  
  219. //������� �� ���������� ������� ����� �� 1 �������
  220. double results(student stud)
  221. {
  222.     double result =0;
  223.     for(int i=0; i<MAX; i++)
  224.         result+=stud.mark[0];
  225.     return result/MAX;
  226. }
  227.  
  228. //������� �� ��������� �� ���������� �� ��������� �������
  229. //  ������� �� ����� ������� �� ������� ���.�
  230. // ������� ����� �� ������ ��������
  231. // ��������� ��� �������� ����
  232. void Find_data(char *filename)
  233. {
  234.     ifstream students;
  235.     student s;
  236.     students.open(filename, ios::in);
  237.  
  238.  
  239.     char ch;
  240.     do
  241.     {
  242.         cout<<"\nI - Information for student\nR - Results for students\n"
  243.             <<"E - End find\nYour choice: ";
  244.         ch = _getch();
  245.         ch=toupper(ch);
  246.         switch(ch)
  247.         {
  248.         case 'I':{cout<<"\nInformation for student\n";//���������� �� 1 �������
  249.                   string aFacNo;
  250.                   cout<<"Enter FacNo: ";
  251.                   getline(cin,aFacNo);
  252.                   students.clear();
  253.                   students.seekg(0);
  254.                    while(students.read(reinterpret_cast<char*>(&s),sizeof(student)))
  255.                     {
  256.                         if(aFacNo.compare(s.facNo)==0) Output(s);
  257.                         if(students.eof()) break;
  258.                     }
  259.                   break;}
  260.         case 'R':{cout<<"\nR-Results for students\n";
  261.                   students.clear();
  262.                   students.seekg(0);
  263.                   while(students.read(reinterpret_cast<char*>(&s),sizeof(student)))
  264.                     {
  265.                         cout<<setw(10)<<setiosflags(ios::left)<<s.facNo
  266.                             <<setw(30)<<setiosflags(ios::left)<<s.Name
  267.                             <<setw(10)<<setprecision(2)
  268.                             <<setiosflags(ios::fixed)<<results(s)<<endl;
  269.                         if(students.eof()) break;
  270.                     }
  271.                   break;}
  272.         case 'E':{cout<<"\nEnd search...\n";break;}
  273.         default:{cout<<"\nIncald choice...\n";break;}
  274.         }
  275.     } while(ch !='E');
  276.     students.close();
  277. }
  278. int main()
  279. {
  280.     char choice;
  281.     do
  282.     {
  283.         choice = Display_menu();
  284.         switch(choice)
  285.         {  
  286.             case 'C': {
  287.                         cout<<"\nCreate empty file\n";
  288.                         Create_file(filename);
  289.                         break;
  290.                       }
  291.             case 'W':{
  292.                         cout<<"\nWrite to file\n";
  293.                         Write_to_file(filename);
  294.                         break;
  295.                       }
  296.             case 'R': {
  297.                         cout<<"\nRead_from_file\n";
  298.                         Read_from_file(filename);
  299.                         break;
  300.                        }
  301.             case 'A': {
  302.                         cout<<"\nAppend a record\n";
  303.                         Append_to_file(filename);
  304.                         break;
  305.                        }
  306.             case 'U': {
  307.                         cout<<"\nUpdete a redord\n";
  308.                         Update_record(filename);
  309.                         break;
  310.                        }
  311.             case 'D': {
  312.                         cout<<"\nDelete a redord\n";
  313.                         Dlete_record(filename);
  314.                         break;
  315.                        }
  316.             case 'F': {cout<<"\nFind data\n";
  317.                         Find_data(filename);
  318.                         break;
  319.                        }
  320.             case 'E' :{cout<<"\nExit program...\n"; break;}
  321.             default:{cout<<"Invalid choice...\n"; break;}
  322.         }
  323.    
  324.     }
  325.     while(choice != 'E');
  326.     cout<<"\nPress any key....";
  327.     _getch();
  328.     return 0;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement