Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.61 KB | None | 0 0
  1. int counter = 0, i, j, ap;
  2. FILE *fp;
  3.  
  4. //count how many numbers there are in the file
  5. if ( ( fp = fopen(filename,"r") )!=NULL )
  6. {
  7. while ( fscanf(fp,"%d", &i)!= EOF ) counter++;
  8. fclose(fp);
  9. }
  10.  
  11. //allocate the matrix; the value of counter is 9
  12. *mat = malloc( sizeof(int*) * sqrt(counter) )
  13. for (i = 0; i < sqrt(counter); i++)
  14. { (*mat)[i] = (int*) malloc( sizeof(int) * sqrt(counter) );}
  15.  
  16. //reopen the file and save the values in the allocated matrix
  17.  
  18. fp = fopen("matrice.txt","r");
  19. for (i = 0; i < sqrt(counter); i++)
  20. {
  21. for (j = 0; j < sqrt(counter); j++) fscanf(fp,"%d", (mat[i])[j]);
  22. }
  23. fclose(fp);
  24.  
  25. return;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement