Guest User

Untitled

a guest
May 22nd, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.15 KB | None | 0 0
  1. //lettura matrice da file,scambiare le riga con colonne e rimetterla in file con percorso inserito da tastiera
  2. #include<iostream>
  3. #include<fstream>
  4. #include<iomanip>
  5. using namespace std;
  6. #define card 100
  7. char percorso1[card];
  8. char percorso2[card];
  9. using namespace std;
  10. typedef int tmatrice[card][card];
  11. tmatrice matrice;
  12. fstream file1,file2;
  13. void lettura_matrice_da_file(fstream &file1,int matrice[][card],int rr,int rc,char percorso1[]);
  14. void stampa_scambio_matrice(int matrice[][card],int rr,int rc);
  15. void scrittura_su_file(fstream &file2,int matrice[][card],int rr,int rc,char percorso2[]);
  16. int rr;
  17. int rc;
  18. int main()
  19. {
  20.     lettura_matrice_da_file(file1,matrice,rr,rc,percorso1);
  21.     stampa_scambio_matrice(matrice,rr,rc);
  22.     scrittura_su_file(file2,matrice,rr,rc,percorso2);
  23.     system("pause");
  24. }
  25.  
  26.  
  27. void lettura_matrice_da_file(fstream &file1, int matrice[][card],int rr,int rc,char percorso1[])
  28. {
  29.      
  30.      cout<<setw(2)<<"Inserisci percorso del file";
  31.      cin.getline(percorso1,card-1,'\n');
  32.      file1.open(percorso1,ios::in);
  33.      //gestione errori
  34.      if(!file1)
  35.      {
  36.                cout<<"Impossibile aprire il file";
  37.                exit(1);
  38.      }
  39.      while(!file1.eof())
  40.      {
  41.                        
  42.         file1>>rr;
  43.         file2>>rc;                
  44.         for(int i=0;i<rr;i++)
  45.         {
  46.                 for(int j=0;j<rc;j++)
  47.                 {
  48.                         file1>>matrice[i][j];
  49.                 }
  50.         }
  51.      }
  52.      
  53.      file1.close();
  54. }
  55.  
  56. void stampa_scambio_matrice(int matrice[][card],int rr,int rc)
  57. {
  58.      for(int i=0;i<rr;i++)
  59.      {
  60.              for(int j=0;j<rc;j++)
  61.              {
  62.                      cout<<matrice[j][i];
  63.              }
  64.              cout<<endl;
  65.      }
  66. }
  67.  
  68. void scrittura_su_file(fstream &file2,int matrice[][card],int rr,int rc,char percorso2[])
  69. {
  70.      cout<<setw(2)<<"Inserire percorso in cui si vuole inserire la matrice";
  71.      cin.getline(percorso2,card-1,'\n');
  72.      file2.open(percorso2,ios::out);
  73.      for(int i=0;i<rr;i++)
  74.      {
  75.              for(int j=0;j<rc;j++)
  76.              {
  77.                      file2<<matrice[j][i];
  78.              }
  79.      }
  80. }
Add Comment
Please, Sign In to add comment