Advertisement
Guest User

ucitaj matricu

a guest
Sep 15th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.71 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. int countLines(FILE *file) {
  4.     int lines = 0;
  5.     int c;
  6.     while (EOF != (c = fgetc(file))) {
  7.         if (c == '\n') {
  8.             ++lines;
  9.         }
  10.     }
  11.     rewind(file);
  12.     return lines;
  13. }
  14.  
  15. int main(void) {
  16.     int mat[5][5];
  17.     FILE * fajl = fopen("matrica.txt", "r");
  18.     if (fajl == NULL) {
  19.         printf("Greska");
  20.         return 0;
  21.     }
  22.     int dim = countLines(fajl);
  23.     printf("%d\n\n", dim);
  24.     for (int i = 0; i < dim; i++)
  25.     {
  26.         for (int j = 0; j < dim; j++)
  27.         {
  28.             if (!fscanf(fajl, "%d", &mat[i][j])) {
  29.                 break;
  30.             }
  31.         }
  32.     }
  33.     for (int i = 0; i < dim; i++)
  34.     {
  35.         for (int j = 0; j < dim; j++)
  36.         {
  37.             printf("mat[%d][%d] = %d\t", i, j, mat[i][j]);
  38.         }
  39.         printf("\n");
  40.     }
  41.  
  42.     return 0;
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement