kat_power

7-20(better ver.)

May 1st, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.28 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <cstdlib>
  4. #include <cstdio>
  5. #include<string.h>
  6. #include <Windows.h>
  7. //exellent code~~
  8.  
  9. struct ORDER
  10. {
  11.     char senacc[20];
  12.     char benacc[20];
  13.     int sumtran;
  14.     char money[20];
  15.     char name[20];
  16. };
  17.  
  18. int main()
  19. {
  20.     setlocale(LC_ALL, "russian");
  21.     SetConsoleCP(1251);//устанавливает кодировку ввода из консоли в редактор кода
  22.     SetConsoleOutputCP(1251);//устанавливает кодировку вывода на коснсоль
  23.  
  24.     char i_benacc[20];
  25.     printf("Введите последние 4 цифры счета получателя:\n");
  26.     gets_s(i_benacc);
  27.    
  28.  
  29.     ORDER *orders;
  30.     FILE *myfile;
  31.  
  32.     char input_name[20];
  33.     printf("\nВведите имя файла:\n");
  34.     gets_s(input_name);
  35.     strcat_s(input_name, ".txt");
  36.     fopen_s(&myfile, input_name, "r");
  37.  
  38.     while (myfile == NULL)
  39.     {
  40.         printf("\nОшибка открытия файла!\nВведите имя файла ещё раз: \n");
  41.         gets_s(input_name);
  42.         strcat_s(input_name, ".txt");
  43.         fopen_s(&myfile, input_name, "r");
  44.     }
  45.  
  46.     int len = 1;
  47.     while (!feof(myfile)) {
  48.         if (fgetc(myfile) == '\n')
  49.             len++;
  50.     }
  51.  
  52.     fseek(myfile, 0, SEEK_SET);
  53.     orders = (ORDER*)malloc(len * sizeof(ORDER));
  54.  
  55.     char line[100];
  56.     char items[5][20];//5- число структур, 20-их размер
  57.  
  58.     for (int currOrder = 0; currOrder < len; currOrder++) {
  59.         fgets(line, 100, myfile);
  60.  
  61.         for (int i = 0, current_item = 0, current_letter = 0; ; i++) {
  62.             if (line[i] == ' ' || line[i] == '\n' || line[i] == '\0') {
  63.                 items[current_item++][current_letter] = '\0';
  64.                 current_letter = 0;
  65.                 if (line[i] == '\n' || line[i] == '\0') break;
  66.             }
  67.             else items[current_item][current_letter++] = line[i];
  68.         }
  69.  
  70.         strcpy_s(orders[currOrder].senacc, items[0]);//коипрование содержимого
  71.         strcpy_s(orders[currOrder].benacc, items[1]);
  72.         orders[currOrder].sumtran = atoi(items[2]);//возвращает интовое значение
  73.         strcpy_s(orders[currOrder].money, items[3]);
  74.         strcpy_s(orders[currOrder].name, items[4]);
  75.     }
  76.  
  77.     FILE *myfile_2;
  78.     char output_name[20];
  79.  
  80.     printf("\nВведите имя нового файла:\n");
  81.     gets_s(output_name);
  82.     strcat_s(output_name, ".txt");
  83.  
  84.     fopen_s(&myfile_2, output_name, "w");
  85.  
  86.     while (myfile_2 == NULL)
  87.     {
  88.         printf("\nОшибка открытия файла!\nВведите имя файла ещё раз:\n");
  89.     }
  90.  
  91.      
  92.     for (int i = 0; i < len; i++)
  93.     {
  94.         int lenBenacc = strlen(orders[i].benacc);
  95.  
  96.         if (orders[i].benacc[lenBenacc - 1] == i_benacc[3]
  97.             && orders[i].benacc[lenBenacc - 2] == i_benacc[2]
  98.             && orders[i].benacc[lenBenacc - 3] == i_benacc[1]
  99.             && orders[i].benacc[lenBenacc - 4] == i_benacc[0])
  100.  
  101.         printf("\n%s %i %s %s\n", orders[i].benacc, orders[i].sumtran, orders[i].money, orders[i].name);
  102.    
  103.         if (orders[i].benacc[lenBenacc - 1] == i_benacc[3]
  104.             && orders[i].benacc[lenBenacc - 2] == i_benacc[2]
  105.             && orders[i].benacc[lenBenacc - 3] == i_benacc[1]
  106.             && orders[i].benacc[lenBenacc - 4] == i_benacc[0])
  107.  
  108.             fprintf(myfile_2, "%s %s %i %s %s\n", orders[i].senacc, orders[i].benacc, orders[i].sumtran, orders[i].money, orders[i].name);
  109.     }
  110.  
  111.     fclose(myfile);
  112.     fclose(myfile_2);
  113.     free(orders);
  114.  
  115.     system("pause");
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment