Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int counter = 0, i, j, ap;
- FILE *fp;
- //count how many numbers there are in the file
- if ( ( fp = fopen(filename,"r") )!=NULL )
- {
- while ( fscanf(fp,"%d", &i)!= EOF ) counter++;
- fclose(fp);
- }
- //allocate the matrix; the value of counter is 9
- *mat = malloc( sizeof(int*) * sqrt(counter) )
- for (i = 0; i < sqrt(counter); i++)
- { (*mat)[i] = (int*) malloc( sizeof(int) * sqrt(counter) );}
- //reopen the file and save the values in the allocated matrix
- fp = fopen("matrice.txt","r");
- for (i = 0; i < sqrt(counter); i++)
- {
- for (j = 0; j < sqrt(counter); j++) fscanf(fp,"%d", (mat[i])[j]);
- }
- fclose(fp);
- return;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement