Advertisement
Guest User

Untitled

a guest
Jun 14th, 2020
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int mat[3][3];
  5.  
  6. int main()
  7. {
  8.     int x, y; //Contatori
  9.     FILE *f; //File
  10.     char directory[50]; //Directory matrice.dat
  11.     char num; //Numero letto da matrice.dat
  12.    
  13.     cout<<"Inserisci directory: ";
  14.     cin>>directory; //Inserisci directory
  15.    
  16.     f = fopen(directory, "rb"); //Apri file
  17.    
  18.     if(f!=NULL) //File aperto
  19.     {
  20.         for(x=0; x<3; x++)
  21.         {
  22.             y = 0;
  23.             cout<<endl;
  24.             while(y<=2)
  25.             {
  26.                 fread(&num, sizeof(num), 1, f);
  27.                 cout<<num<<endl; //Stampa char
  28.                 if(num!=' ')
  29.                 {
  30.                     mat[x][y] = num-'0';
  31.                     y++;
  32.                 }
  33.             }
  34.         }
  35.     }
  36.     else
  37.         cout<<"Errore!\n";
  38.    
  39.     //Stampa matrice
  40.     for(x=0; x<3; x++)
  41.     {
  42.         cout<<endl;
  43.         for(y=0; y<3; y++)
  44.             cout<<mat[x][y]<<'\t';
  45.     }
  46.    
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement