I have to read from a file multiple records(each components of the records are separated by a comma), and I can't understand what's the problem, so here's my file:
Rossi,Mario,M,mariorossi@gmail.com,3923333332,Portiere Bianchi,Giuseppe,M,giuseppebianchi@gmail.com,3470000021,Attaccante Ferrari,Anna,F,annaferrari@gmail.com,3466482645,Attaccante Romano,Antonio,M,antonioromano@gmail.com,3450394672,Centrocampista
and here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct dati_giocatori {
char cognome[20];
char nome[20];
char genere[20];
char email[50];
char telefono[16];
char ruolo[20];
};
typedef struct dati_giocatori GIOCATORE;
void stampa_file(FILE *pfile, GIOCATORE *vettore, int dim, char *stringa);
int main (){
FILE *pfile;
GIOCATORE *vettore;
int dim=0;
char stringa[200];
printf("Quanti giocatori vuoi visualizzare?");
scanf("%d",&dim);
vettore=(GIOCATORE*)malloc(dim*sizeof(GIOCATORE));
pfile=fopen("Giocatori.txt","r");
stampa_file(pfile,vettore,dim,stringa);
system("pause");
fclose(pfile);
pfile=fopen("Giocatoriv.txt","r");
system("pause");
free(vettore);
fclose(pfile);
system("pause");
return 0;
system("pause");
}
void stampa_file(FILE *pfile, GIOCATORE *vettore, int dim, char *stringa){
int i=0;
int j=0;
if(pfile!=NULL){
if(!feof(pfile)){
while(i<dim){
if(!feof(pfile)){
fgets(stringa,200,pfile);
sscanf(stringa,"%[^,],%[^,],%[^,],%[^,],%[^,],%s",vettore[i].cognome,vettore[i].nome,
vettore[i].genere,vettore[i].email,vettore[i].telefono,vettore[i].ruolo);
i++;
}
else{
printf("\n----- Giocatori finiti -----\n");
printf("\n");
i=dim;
}
}
}
else{
printf("\nFile finito.\n");
}
}
else{
printf("Errore nell'apertura del file.\n");
printf("\n");
}
while(j<dim){
printf("%s,%s,%s,%s,%s,%s\n",vettore[j].cognome,vettore[j].nome,
vettore[j].genere,vettore[j].email,vettore[j].telefono,vettore[j].ruolo);
j++;
}
system("pause");
}
I know the problem is with the sscanf()
, because it prints on screen just the first component of every record, and five commas, but I can't figure out how to solve this problem... It does not assign the right data to the right place on the records, is the sscanf()
format correct? I am not very familiar with delimiters, how should I fix this?
Thanks everyone for helping. I'm sorry you waited so long for the response but I could not edit yesterday. I'm really sorry also for the posting errors, I'm new at stack overflow and I'm also new in C (it's my first language)...(and just to make it difficult to me, I'm also not a native speaker as you can notice by my grammar errors). Yes the email addesses are fake. By the way, I'm gonna be honest I don't know hot to check if the file has a newline character at the end of every record...While writing the file I thought that pressing "enter" would gave to me the newline character, but reading you comments I am not sure anymore. I want to use newline character at the end of every record, but how do I do that? and, will this make my code work? (I opened a second time the file because I was just trying other things on it, so please don't mind about that, same for the system pause).