Advertisement
annie_02

zad3

Apr 1st, 2022
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.50 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #pragma warning(disable:4996)
  4. const int MAX_FIELD_LENGTH = 50;
  5. const int MAX_BUFFER_SIZE = 128;
  6.  
  7. struct studentOOP {
  8.     char firstName[MAX_FIELD_LENGTH];
  9.     char secondName[MAX_FIELD_LENGTH];
  10.     char email[MAX_FIELD_LENGTH];
  11.     int fn;
  12. };
  13.  
  14. size_t getStudentsNumber(const char* fileName) {
  15.     size_t counter = 0;
  16.     std::ifstream file(fileName);
  17.  
  18.     if (!file.is_open()) {
  19.         std::cout << "Error while opening the file.";
  20.         return -1;
  21.     }
  22.  
  23.     while (!file.eof()) {
  24.         char line[MAX_BUFFER_SIZE];
  25.         file.getline(line, MAX_BUFFER_SIZE);
  26.         counter++;
  27.     }
  28.  
  29.     file.close();
  30.     return counter;
  31. }
  32.  
  33.  
  34. void readStudent(const char* line, studentOOP& student) {
  35.     size_t index = 0;
  36.  
  37.     while (line[index] != ',') {
  38.         student.firstName[index] = line[index];
  39.         index++;
  40.     }
  41.     student.firstName[index] = '\0';
  42.     index++;
  43.  
  44.     size_t secondNameCounter = 0;
  45.     while (line[index] != ',') {
  46.         student.secondName[secondNameCounter++] = line[index++];
  47.     }
  48.     student.secondName[secondNameCounter] = '\0';
  49.     index++;
  50.  
  51.     size_t emailCounter = 0;
  52.     while (line[index] != ',') {
  53.         student.email[emailCounter++] = line[index++];
  54.     }
  55.     student.email[emailCounter] = '\0';
  56.     index++;
  57.  
  58.     size_t fnCounter = 0;
  59.     char fnStr[MAX_FIELD_LENGTH];
  60.     while (line[index] != '\0') {
  61.         fnStr[fnCounter++] = line[index++];
  62.     }
  63.     fnStr[fnCounter] = '\0';
  64.     student.fn = atoi(fnStr);
  65. }
  66.  
  67. size_t indexUntilFirstSpaceOrTerminatingNull(const char* input) {
  68.     size_t counter = 0;
  69.     while (*input != ' ' && *input != '\t' && *input != '\0') {
  70.         ++counter;
  71.         ++input;
  72.     }
  73.     return counter;
  74. }
  75.  
  76. bool printStudentInfo(studentOOP* students, size_t stCount, int fn) {
  77.  
  78.     for (int i = 0; i < stCount; ++i) {
  79.         if (students[i].fn == fn) {
  80.             std::cout << "Name = " << students[i].firstName
  81.                 << " " << students[i].secondName
  82.                 << ", email = " << students[i].email
  83.                 << ", FN = " << students[i].fn << std::endl;
  84.  
  85.             return true;
  86.         }
  87.     }
  88.     return false;
  89. }
  90.  
  91. bool saveStudentsToFile(studentOOP* students, size_t stCount, const char* fileName) {
  92.  
  93.     std::ofstream file(fileName);
  94.  
  95.     if (!file.is_open()) {
  96.         return false;
  97.     }
  98.  
  99.     file << "First Name, Second Name, Email, FN" << std::endl;
  100.  
  101.     for (int i = 0; i < stCount; ++i) {
  102.         file << students[i].firstName
  103.             << "," << students[i].secondName
  104.             << "," << students[i].email
  105.             << "," << students[i].fn;
  106.         if (i != stCount - 1) {
  107.             file << std::endl;
  108.         }
  109.     }
  110.     return true;
  111. }
  112.  
  113. bool editStudent(studentOOP* students, size_t stCount, int fn, const char* newName) {
  114.  
  115.     for (int i = 0; i < stCount - 1; ++i) {
  116.         if (students[i].fn == fn) {
  117.             strcpy(students[i].firstName, newName);
  118.             return true;
  119.         }
  120.     }
  121.     return false;
  122. }
  123.  
  124. int main()
  125. {
  126.     std::cout << "Open file." << std::endl << ">";
  127.     char fileName[MAX_FIELD_LENGTH];
  128.     std::cin.getline(fileName, MAX_FIELD_LENGTH);
  129.  
  130.  
  131.     size_t studentsCount = getStudentsNumber(fileName) - 1;
  132.  
  133.     if (studentsCount == 0) {
  134.         std::cout << "Error while reading data.";
  135.         return -1;
  136.     }
  137.  
  138.     studentOOP* students = new studentOOP[studentsCount];
  139.  
  140.     std::ifstream file(fileName);
  141.  
  142.     if (!file.is_open()) {
  143.         std::cout << "Error while opening the file.";
  144.         return -1;
  145.     }
  146.  
  147.  
  148.     int counter = 1;
  149.     while (!file.eof()) {
  150.         char line[MAX_BUFFER_SIZE];
  151.         file.getline(line, MAX_BUFFER_SIZE);
  152.  
  153.         if (counter == 1) {
  154.             counter++;
  155.             continue;
  156.         }
  157.  
  158.         readStudent(line, students[counter - 2]);
  159.         counter++;
  160.     }
  161.     std::cout << "File has been opened successfully." << std::endl;
  162.     file.close();
  163.  
  164.     char commandLine[MAX_FIELD_LENGTH];
  165.  
  166.     do {
  167.         std::cout << ">";
  168.  
  169.         std::cin.getline(commandLine, MAX_FIELD_LENGTH);
  170.  
  171.         size_t lineLength = strlen(commandLine);
  172.  
  173.         size_t firstArgSize = indexUntilFirstSpaceOrTerminatingNull(commandLine);
  174.         char* firstArgument = new char[firstArgSize + 1];
  175.  
  176.         for (size_t i = 0; i < firstArgSize; i++) {
  177.             firstArgument[i] = commandLine[i];
  178.         }
  179.         firstArgument[firstArgSize] = '\0';
  180.  
  181.         if (lineLength == strlen(firstArgument)) {
  182.             continue;
  183.         }
  184.  
  185.         size_t secondArgSize = lineLength - firstArgSize - 1;
  186.         char* secondArgument = new char[secondArgSize + 1];
  187.  
  188.         for (size_t i = 0; i < secondArgSize; ++i) {
  189.             secondArgument[i] = commandLine[firstArgSize + i + 1];
  190.         }
  191.         secondArgument[secondArgSize] = '\0';
  192.  
  193.         if (strcmp(firstArgument, "edit") == 0) {
  194.  
  195.             size_t separator = indexUntilFirstSpaceOrTerminatingNull(secondArgument);
  196.  
  197.             size_t thirdArgSize = secondArgSize - separator;
  198.             char* thirdArgument = new char[thirdArgSize + 1];
  199.  
  200.             for (int i = 0; i < thirdArgSize; ++i) {
  201.                 thirdArgument[i] = secondArgument[separator + 1 + i];
  202.             }
  203.             thirdArgument[thirdArgSize] = '\0';
  204.  
  205.             int fn = atoi(secondArgument);
  206.  
  207.             bool res = editStudent(students, studentsCount, fn, thirdArgument);
  208.  
  209.             if (!res) {
  210.                 std::cout << "Student with " << fn << " cannot be found!";
  211.             }
  212.  
  213.             delete[] thirdArgument;
  214.         }
  215.         else if (strcmp(firstArgument, "print") == 0) {
  216.             int fn = atoi(secondArgument);
  217.  
  218.             bool res = printStudentInfo(students, studentsCount, fn);
  219.  
  220.             if (!res) {
  221.                 std::cout << "Student with FN " << fn << " cannot be found."
  222.                     << std::endl;
  223.             }
  224.  
  225.         }
  226.         else if (strcmp(firstArgument, "save") == 0) {
  227.             bool res = saveStudentsToFile(students, studentsCount, secondArgument);
  228.  
  229.             if (!res) {
  230.                 std::cout << "Error saving the students!" << std::endl;
  231.             }
  232.             else {
  233.                 std::cout << "Students have been successfully saved." << std::endl;
  234.             }
  235.  
  236.         }
  237.  
  238.         delete[] firstArgument;
  239.         delete[] secondArgument;
  240.  
  241.     } while (strcmp(commandLine, "quit"));
  242.  
  243.  
  244.  
  245.     delete[] students;
  246.     return 0;
  247. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement