Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

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:UsersASUSDocumentsNumerical Methods labsphere projection
ead_values_of-sphere.txt", "r");


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

printf("%d
",n);

sphere sp[n];

for(int i=0;i<n;i++){
	fscanf(fp, "%f %f %f %f
", &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
", sp[i].c);
}
 
PREVIOUS NEXT
Tagged: #read #array #file
ADD COMMENT
Topic
Name
7+7 =