0

this program should take in input a number n and so create n triangles. After, I want to write this triangles in a file and to print this file on standard output. I don't understand why if I input n = 2 the program prints only one triangle's coordinates. Moreover if I input coordinates: 1.0,1.1-1.5,1.6-2.4,2.5 for the triangle's vertices, output is 2.4,2.5-0.0,0.0-0.0,0.0. Can someome help me?

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct{

    float x;
    float y;

} Punto;

typedef struct{

    Punto p1;
    Punto p2;
    Punto p3;

} Triangolo;

Punto creaPunto();
void creaTriangolo(Triangolo *);
void stampaPunto(Punto);
void stampaTriangolo(Triangolo);
void creaFileTriangoli(char[], int);
void stampaFileTriangolo(char[]);

Punto creaPunto(){

    Punto p;

    printf("Inserire la coordinata x: ");
    scanf("%f", &p.x);
    printf("Inserire la coordinata y: ");
    scanf("%f", &p.y);

    return p;
}
void creaTriangolo(Triangolo *t){

    printf("Triangolo.\n");

    printf("Vertice p1:\n");
    t->p1 = creaPunto();
    printf("Vertice p2:\n");
    t->p2 = creaPunto();
    printf("Vertice p3:\n");
    t->p3 = creaPunto();
}


void stampaPunto(Punto v){

    printf("Coordinata x: %f\n", v.x);
    printf("Coordinata y: %f\n", v.y);
}

void stampaTriangolo(Triangolo t){

    printf("Triangolo\n");
    printf("Vertice p1:\n");
    stampaPunto(t.p1);
    printf("Vertice p2:\n");
    stampaPunto(t.p2);
    printf("Vertice p3:\n");
    stampaPunto(t.p3);
}

void creaFileTriangoli(char f[], int n){

    FILE *ptr;
    Triangolo t;
    int i;

    ptr = fopen(f, "wb");

    if(ptr == NULL){

        printf("File inesistente.\n");
    }
    else{

        for(i=0; i<n; i++){

            creaTriangolo(&t);
            fwrite(&t, sizeof(Triangolo), 1, ptr);
        }
    }
}


void stampaFileTriangolo(char f[]){

    FILE *ptr;
    Triangolo t;

    ptr = fopen(f, "rb");

    if(ptr == NULL){

        printf("File inesistente.\n");
    }
    else{

        fseek(ptr, sizeof(Triangolo), SEEK_SET);

        while(!feof(ptr)){


            fread(&t, sizeof(Triangolo), 1, ptr);
            stampaTriangolo(t);

        }

        fclose(ptr);

    }

}

int main(int argc, char *argv[]) {

    FILE *ptr;
    char f[300] = "C:\\sole.dat";
    Triangolo t;
    int n;
    float perimetro;

    printf("Inserire il numero di triangoli da analizzare: ");
    scanf("%d", &n);

    creaFileTriangoli(f,n);
    stampaFileTriangolo(f);

    return 0;
}
Malgiolio
  • 3
  • 1
  • 1
    Please create an _[MCVE](http://stackoverflow.com/help/mcve)_ – bzeaman Jun 22 '16 at 11:42
  • I think is MCVE yet – Malgiolio Jun 22 '16 at 11:43
  • See [Why is “while ( !feof (file) )” always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). You also fail to check any return values from `fwrite()` or `fread()` - and you also need to check the return value from `fclose()`. How do you know the `fwrite()` calls succeeded? – Andrew Henle Jun 22 '16 at 11:44
  • 1
    What is the purpose of `fseek(ptr, sizeof(Triangolo), SEEK_SET);`? This line causes you to skip the first triangle. – s7amuser Jun 22 '16 at 11:51
  • @s7amuser i thought that I could read the file from the start with that `fseek` . I try to delete it,but output is the same. – Malgiolio Jun 22 '16 at 11:54
  • write `fclose` at `creaFileTriangoli` – BLUEPIXY Jun 22 '16 at 12:07
  • @BLUEPIXY I do it, bur output is the same – Malgiolio Jun 22 '16 at 12:11
  • It seems to work correctly if you've already fixed the place pointed out. Try the folder is not a root folder. – BLUEPIXY Jun 22 '16 at 12:26

1 Answers1

1

There are two errors in stampaFileTriangolo, the first is that you skip the first record by using fseek. Note that when a file is opened, the file pointer is at the start of the file, and after you use fread, the file pointer is advanced automatically. So you don't need to call fseek to read a file sequentially.

The second is the abuse of feof, which does not do what many people think. It tells you after you have read beyond the file, not when there is no more data. I suggest using the return value from fread to control the loop, like this

void stampaFileTriangolo(char f[]){
    FILE *ptr;
    Triangolo t;
    ptr = fopen(f, "rb");
    if(ptr == NULL) {
        printf("File inesistente.\n");
    }
    else {
        while (fread(&t, sizeof(Triangolo), 1, ptr) == 1) {
            stampaTriangolo(t);
        }
        fclose(ptr);
    }
}

There is an error also in creaFileTriangoli - you have not closed the file. This may have caused you to think you needed an fseek here.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56