I made a function where I pass as argument two integers and two pointers of two struct that I created in order to write the return in a txt file.
When running the program, if I give printf()
to the return of the function in main
it gives the desired value, but if fprintf()
no longer gives this value. How can I resolve?
char* primeiraPessoa(int countV, int countF, Visitante *visitante, Funcionario *funcionario){
int hora=0;
char *id;
id = "N/A";
for(int i=0;i<countV;i++){
if(i==0){
sprintf(id, "%d", visitante[i].id);
hora=visitante[i].horaE;
}else if(hora>visitante[i].horaE){
sprintf(id, "%d", visitante[i].id);
hora=visitante[i].horaE;
}else if(hora==visitante[i].horaE){
strcat(id,",");
char idTemp[5];
sprintf(idTemp, "%d", visitante[i].id);
strcat(id,idTemp);
}
}
for(int i=0;i<countF;i++){
if(hora>funcionario[i].horaE){
sprintf(id, "%d", funcionario[i].id);
hora=visitante[i].horaE;
}else if(hora==funcionario[i].horaE){
strcat(id,",");
char idTemp[5];
sprintf(idTemp, "%d", funcionario[i].id);
strcat(id,idTemp);
}
}
return id;
}
To write to the txt file I have the function:
void escreveFicheiro(Visitante *visitante,Funcionario *funcionario,int countV,int countF,int countInvalidas,int countBarradas){
char nome [] = "estatisticas.txt";
FILE * fp = fopen(nome, "w");
if (fp) {
fprintf(fp,"15 - %s\n",primeiraPessoa(countV,countF,visitante,funcionario));
}
fclose(fp);
}