Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <conio.h>
- #define BUF 11
- int main() {
- char inp[] = "matrix.txt", exp[] = "result.txt", temp[BUF];
- int summa = 0, element, not_found, not_create, not_established, is_corrupted;
- size_t size = 0, n, m;
- FILE * in, * out;
- errno_t err;
- not_found = 0;
- err = fopen_s(&in, inp, "r");
- if (err != 0) {
- printf("The file is not found!");
- ++not_found;
- } else {
- not_create = 0;
- err = fopen_s(&out, exp, "w");
- if (err != 0) {
- printf("The file is not created!");
- ++not_create;
- } else {
- not_established = 0;
- fscanf_s(in, "%s", temp, BUF);
- if (!(size = atoi(temp))) {
- printf("The size is not established!");
- ++not_established;
- } else {
- is_corrupted = 0;
- for (n = 0; n < size; n++) {
- for (m = 0; m < size; m++) {
- fscanf_s(in, "%s", temp, BUF);
- if (n == m) {
- if (!(element = atoi(temp))) {
- printf("The file is corrupted!");
- ++is_corrupted;
- break;
- } summa += element;
- }
- } if (is_corrupted) break;
- }
- if (!is_corrupted) {
- _itoa_s(summa, temp, BUF, 10);
- fputs(temp, out);
- }
- } fclose(out);
- } fclose(in);
- }
- if (not_found || not_create || not_established || is_corrupted) _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement