Advertisement
Kentoo

Zh#1

Dec 23rd, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <conio.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string>
  6.  
  7. #pragma warning (disable:4996)
  8.  
  9. bool CreateNewFile(char * FileName, int stQuantity);
  10.  
  11. bool PrintFile(char * FileName);
  12.  
  13. bool WriteNewFileBySgroup(char * readFileName, char * writeFileName, char* group);
  14.  
  15. struct Student {
  16.     char group[30];
  17.     char fam[50];
  18.     int stip;
  19. };
  20.  
  21. int main()
  22. {
  23.     int StQuantity(0);
  24.     char Sfile[100], ESfile[100];
  25.     printf("Input Students file filename: ");
  26.     gets_s(Sfile);
  27.     printf("Input quantity of students: ");
  28.     scanf("%d", &StQuantity);
  29.     printf("\n");
  30.     getchar();
  31.  
  32.     bool checkStatus = CreateNewFile(Sfile, StQuantity);
  33.  
  34.     if (checkStatus) {
  35.         printf("File was created!\n");
  36.     }
  37.     else {
  38.         printf("Error occured!\n");
  39.         return -1;
  40.     }
  41.  
  42.     checkStatus = PrintFile(Sfile);
  43.     if (checkStatus) {
  44.         printf("File was read!\n");
  45.     }
  46.     else {
  47.         printf("Error occured!\n");
  48.         return -1;
  49.     }
  50.  
  51.     printf("\n");
  52.  
  53.     char group[30];
  54.  
  55.     printf("Input specified group of students: ");
  56.     scanf("%s", &group);
  57.  
  58.     printf("\nInput new students file filename: ");
  59.     scanf("%s", &ESfile);
  60.  
  61.     checkStatus = WriteNewFileBySgroup(Sfile, ESfile, group);
  62.     if (checkStatus) {
  63.         printf("File was created!\n");
  64.     }
  65.     else {
  66.         printf("Error occured!\n");
  67.         return -1;
  68.     }
  69.  
  70.     checkStatus = PrintFile(ESfile);
  71.     if (checkStatus) {
  72.         printf("File was read!\n");
  73.     }
  74.     else {
  75.         printf("Error occured!\n");
  76.         return -1;
  77.     }
  78.  
  79.     system("pause");
  80.  
  81.     return 0;
  82. }
  83.  
  84. bool CreateNewFile(char * FileName, int stQuantity) {
  85.     FILE * fileStream = fopen(FileName, "wb");
  86.     if (!fileStream) {
  87.         return false;
  88.     }
  89.     Student st;
  90.     for (int i = 0; i < stQuantity; i++) {
  91.         printf("Input student group: ");
  92.         scanf("%s", &st.group);
  93.         printf("Input fam of student: ");
  94.         scanf("%s", &st.fam);
  95.         printf("Input stip of student: ");
  96.         scanf("%d", &st.stip);
  97.         fwrite(&st, sizeof(st), 1, fileStream);
  98.         printf("\n");
  99.     }
  100.     fclose(fileStream);
  101.     return true;
  102. }
  103.  
  104. bool PrintFile(char * FileName) {
  105.     FILE * fileStream = fopen(FileName, "rb");
  106.     if (!fileStream) {
  107.         return false;
  108.     }
  109.     Student st;
  110.     while (fread(&st, sizeof(st), 1, fileStream)) {
  111.         printf("%s : %s : %d\n", st.group, st.fam, st.stip);
  112.     }
  113.     fclose(fileStream);
  114.     return true;
  115. }
  116.  
  117. bool WriteNewFileBySgroup(char * readFileName, char * writeFileName, char* group) {
  118.     FILE * readFileStream = fopen(readFileName, "rb");
  119.     FILE * writeFileStream = fopen(writeFileName, "wb");
  120.  
  121.     if ((!readFileStream) || (!writeFileStream)) {
  122.         return false;
  123.     }
  124.  
  125.     Student st;
  126.     while (fread(&st, sizeof(st), 1, readFileStream)) {
  127.         if (strcmp(st.group, group) == 0 && st.stip > 0) {        
  128.             fwrite(&st, sizeof(st), 1, writeFileStream);
  129.         }
  130.     }
  131.  
  132.     fclose(readFileStream);
  133.     fclose(writeFileStream);
  134.     return true;
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement