Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <cstdlib>
- #include <cstdio>
- #include<string.h>
- #include <Windows.h>
- //exellent code~~
- struct ORDER
- {
- char senacc[20];
- char benacc[20];
- int sumtran;
- char money[20];
- char name[20];
- };
- int main()
- {
- setlocale(LC_ALL, "russian");
- SetConsoleCP(1251);//устанавливает кодировку ввода из консоли в редактор кода
- SetConsoleOutputCP(1251);//устанавливает кодировку вывода на коснсоль
- char i_benacc[20];
- printf("Введите последние 4 цифры счета получателя:\n");
- gets_s(i_benacc);
- ORDER *orders;
- FILE *myfile;
- char input_name[20];
- printf("\nВведите имя файла:\n");
- gets_s(input_name);
- strcat_s(input_name, ".txt");
- fopen_s(&myfile, input_name, "r");
- while (myfile == NULL)
- {
- printf("\nОшибка открытия файла!\nВведите имя файла ещё раз: \n");
- gets_s(input_name);
- strcat_s(input_name, ".txt");
- fopen_s(&myfile, input_name, "r");
- }
- int len = 1;
- while (!feof(myfile)) {
- if (fgetc(myfile) == '\n')
- len++;
- }
- fseek(myfile, 0, SEEK_SET);
- orders = (ORDER*)malloc(len * sizeof(ORDER));
- char line[100];
- char items[5][20];//5- число структур, 20-их размер
- for (int currOrder = 0; currOrder < len; currOrder++) {
- fgets(line, 100, myfile);
- for (int i = 0, current_item = 0, current_letter = 0; ; i++) {
- if (line[i] == ' ' || line[i] == '\n' || line[i] == '\0') {
- items[current_item++][current_letter] = '\0';
- current_letter = 0;
- if (line[i] == '\n' || line[i] == '\0') break;
- }
- else items[current_item][current_letter++] = line[i];
- }
- strcpy_s(orders[currOrder].senacc, items[0]);//коипрование содержимого
- strcpy_s(orders[currOrder].benacc, items[1]);
- orders[currOrder].sumtran = atoi(items[2]);//возвращает интовое значение
- strcpy_s(orders[currOrder].money, items[3]);
- strcpy_s(orders[currOrder].name, items[4]);
- }
- FILE *myfile_2;
- char output_name[20];
- printf("\nВведите имя нового файла:\n");
- gets_s(output_name);
- strcat_s(output_name, ".txt");
- fopen_s(&myfile_2, output_name, "w");
- while (myfile_2 == NULL)
- {
- printf("\nОшибка открытия файла!\nВведите имя файла ещё раз:\n");
- }
- for (int i = 0; i < len; i++)
- {
- int lenBenacc = strlen(orders[i].benacc);
- if (orders[i].benacc[lenBenacc - 1] == i_benacc[3]
- && orders[i].benacc[lenBenacc - 2] == i_benacc[2]
- && orders[i].benacc[lenBenacc - 3] == i_benacc[1]
- && orders[i].benacc[lenBenacc - 4] == i_benacc[0])
- printf("\n%s %i %s %s\n", orders[i].benacc, orders[i].sumtran, orders[i].money, orders[i].name);
- if (orders[i].benacc[lenBenacc - 1] == i_benacc[3]
- && orders[i].benacc[lenBenacc - 2] == i_benacc[2]
- && orders[i].benacc[lenBenacc - 3] == i_benacc[1]
- && orders[i].benacc[lenBenacc - 4] == i_benacc[0])
- fprintf(myfile_2, "%s %s %i %s %s\n", orders[i].senacc, orders[i].benacc, orders[i].sumtran, orders[i].money, orders[i].name);
- }
- fclose(myfile);
- fclose(myfile_2);
- free(orders);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment