Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void get_sizes(char ** file_name,int * r ,int * c)
- {
- //getting number of columns and rows
- int flag = 0 ;
- FILE *f;
- f = fopen(file_name,"r");
- if(f != NULL )
- {
- while (!feof(f))
- {
- char ch = fgetc(f);
- if(ch == ' ' && flag == 0)
- {
- *c = *c + 1 ;
- }
- if(ch == '\n')
- {
- *r = *r + 1 ;
- flag = 1; //flag to stop counting spaces
- }
- }
- fclose(f);
- }
- else
- {
- printf("error reading file");
- exit(-1);
- }
- }
- float ** build_matrix(char ** file_name,int * r ,int * c )
- {
- FILE *f;
- float temp[50];
- int rows = *r , columns = *c;
- int counter = 0,i = 0,j = 0;
- //dynamic allocation for matrix
- float **matrix;
- matrix = malloc(sizeof(float*) * columns);
- for(i = 0; i < rows; i++) {
- matrix[i] = malloc(sizeof(float*) * columns);
- }
- //getting the values in a temporary array
- f = fopen(file_name, "r");
- if(f != NULL )
- {
- while (!feof(f))
- {
- fscanf(f,"%f",&temp[counter]);
- counter++;
- }
- // building the matrix
- counter = 0 ;
- for(i = 0; i < *r; i++)
- {
- for(j = 0; j < *c; j++)
- {
- matrix[i][j] = temp[counter];
- counter++;
- }
- }
- }
- else
- {
- printf("error reading file");
- exit(-1);
- }
- fclose(f);
- return matrix;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement