I have this [UPDATED]
typedef struct Cerchi {
char nome[4];
int x; //coordinate centro
int y; //coordinate centro
int r; //raggio
}cerchio;
cerchio *leggi_file ( FILE *fp)
{
char buffer[100];
int i=0;
cerchio *bufcer;
bufcer=(cerchio *)malloc(sizeof (int)*10000000);
while(fgets(buffer, sizeof(buffer), fp)!= NULL) //Fino a che file non รจ null
{
//bufcer=realloc(bufcer, sizeof(int)*100);
sscanf(buffer, "%s %d %d %d",bufcer[i].nome,&bufcer[i].x,&bufcer[i].y,&bufcer[i].r);
/*stampa di controllo*/ printf("\n%s %d %d %d",bufcer[i].nome,bufcer[i].x,bufcer[i].y,bufcer[i].r);
i++;
}
return bufcer;
}
This function is working. That's the UPDATED main
int main(int argc, char *argv[]) {
FILE *fp;
if (argc < 2) {
printf("Mancano parametri da tastiera\n"); //Sempre >=1 parametri passati
exit(1);
}
fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("Impossibile aprire il file\n");
exit(1);
}
struct Cerchi *bufcer = NULL;
bufcer = leggi_file(fp);
stampa(bufcer);
//vettore = leggi_file(FILE *fp); E R R O R E
fclose(fp);
return 0;
}
I'm not able to print my *bufcer struct in this function
void stampa(bufcer)
{
int i;
for (i=0;i<50;i++)
{
printf("\n%s %d %d %d",bufcer[i]->nome,bufcer[i]->x,bufcer[i]->y,bufcer[i]->r);
}
}
Please try to help me, tomorrow morning I have an exam about it [UPDATED] Which error I'm doing? Can you try to solve it and help me? Thanks a lot...
malloc()
.sscanf()
to ensure all the elements got scanned successfully.stampa( struct Cerchi *bufcer);
tostampa(bufcer);
This is a function call not a function definition or declaration. Also, addstruct Cerchi *bufcer = NULL;
insidemain()
before callingstampa()
.You are not using the return value of
leggi_file ()
. As per your logic, you need to collect the value inbufcer
. Change your code tomalloc()
before using the returned pointer.