Answers for "how to read 2d array from a file in c"

C
0

how to read 2d array from a file in c

/*
File structure:
3
150 250 150 20
180 -240 -150 150
200 0 0 200
*/

typedef struct{
    float r;
    float a;
    float b;
    float c;
} sphere; //sphere parameter

FILE *fp;

fp = fopen("C:\\Users\\ASUS\\Documents\\Numerical Methods lab\\sphere projection\\read_values_of-sphere.txt", "r");


int n;
fscanf(fp, "%d\n",&n);

printf("%d\n",n);

sphere sp[n];

for(int i=0;i<n;i++){
	fscanf(fp, "%f %f %f %f\n", &sp[i].r, &sp[i].a, &sp[i].b, &sp[i].c);
}

for(int i=0;i<n;i++){
  printf("%f ", sp[i].r);
  printf("%f ", sp[i].a);
  printf("%f ", sp[i].b);
  printf("%f\n", sp[i].c);
}
Posted by: Guest on April-07-2022

Code answers related to "C"

Browse Popular Code Answers by Language