Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // structure
- typedef struct coordinates{
- double **a;
- } coord;
- // counts the nmbr of points
- int count(){
- int i;
- int a1, a2, a3;
- FILE *fp;
- fp = fopen("abc.txt", "r+");
- while (fscanf(fp, "%d %d %d", &a1, &a2, &a3) != EOF){
- i++;
- if (feof(fp)){
- break;
- }
- }
- fclose(fp);
- return(i);
- }
- // creates new structure with the right size of memory allocated
- coord *newVector(size_t s){
- coord *v;
- int j;
- v = malloc(sizeof(coord));
- v->a = malloc(sizeof(double*)*3);
- for (j=0; j<3; j++){
- v->a[j] = malloc(sizeof(double)*s);
- }
- }
- void get_coords(coord *points){
- int i=0;
- FILE *fp;
- fp = fopen("abc.txt", "r+");
- while (fscanf(fp, "%le %le %le", &points->a[i][0], &points->a[i][1], &points->a[i][2]) != EOF){
- i++;
- }
- fclose(fp);
- }
- int main(){
- int i = 0, j=0;
- coord *points;
- i = count();
- points = newVector(i);
- get_coords(points);
- for (i=0; i<3; i++){
- printf("%lf %lf %lfn", points->a[i][0], points->a[i][1], points->a[i][2]);
- }
- }
- 1 2 3
- 4 5 6
- 7 8 9
Add Comment
Please, Sign In to add comment