Advertisement
NickAndNick

Файлы. Сумма элементов главной диагонали матрицы

Nov 20th, 2012
236
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.73 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <conio.h>
  4. #define BUF 11
  5. int main() {
  6.     char inp[] = "matrix.txt", exp[] = "result.txt", temp[BUF];
  7.     int summa = 0, element, not_found, not_create, not_established, is_corrupted;
  8.     size_t size = 0, n, m;
  9.     FILE * in, * out;
  10.     errno_t err;
  11.     not_found = 0;
  12.     err = fopen_s(&in, inp, "r");
  13.     if (err != 0) {
  14.         printf("The file is not found!");
  15.         ++not_found;
  16.     } else {
  17.         not_create = 0;
  18.         err = fopen_s(&out, exp, "w");    
  19.         if (err != 0) {
  20.             printf("The file is not created!");
  21.             ++not_create;
  22.         } else {
  23.             not_established = 0;
  24.             fscanf_s(in, "%s", temp, BUF);
  25.             if (!(size = atoi(temp))) {
  26.                 printf("The size is not established!");
  27.                 ++not_established;
  28.             } else {
  29.                 is_corrupted = 0;
  30.                 for (n = 0; n < size; n++) {
  31.                     for (m = 0; m < size; m++) {
  32.                         fscanf_s(in, "%s", temp, BUF);
  33.                         if (n == m) {
  34.                             if (!(element = atoi(temp))) {
  35.                                 printf("The file is corrupted!");
  36.                                 ++is_corrupted;
  37.                                 break;
  38.                             } summa += element;
  39.                         }
  40.                     } if (is_corrupted) break;
  41.                 }
  42.                 if (!is_corrupted) {
  43.                     _itoa_s(summa, temp, BUF, 10);
  44.                     fputs(temp, out);
  45.                 }
  46.             } fclose(out);
  47.         } fclose(in);
  48.     }
  49.     if (not_found || not_create || not_established || is_corrupted) _getch();
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement