Reputation: 11
I'm trying to read some strings from a txt file. Each line has a 5 digit number, a full name that occupies 51 characters (including spaces), 2 city names, each one occupyng 11 characters including spaces, and one number. Example:
12345António Manuel Silva Mendes Frankfurt Varsovia 1
I can now scan the first 2 strings, but I can't scan the city names, nor the last number.
Here's the struct:
typedef struct bilhete{
int BI;
char nome[51];
char partida[11];
char chegada[11];
int data[2];
}BILHETE;
here's the way I'm reading the file
while(!feof(fp)){
fscanf(fp,"%d%51[^\n]s%11c%11c%d\n", &bilhete.BI, bilhete.nome, bilhete.partida, bilhete.chegada, &bilhete.data);
What am I doing wrong? when I print the city names, nothing appears!
Upvotes: 1
Views: 72
Reputation: 1313
Since you have fixed columns, I would consider just reading in the data and doing my own scanning with something like:
char linein[200];
char *p;
while (fgets(linein, sizeof(linein), fp))
{
char biBuf[10]={0};
char dataBuf[10]={0};
p=linein;
memcpy(biBuf,linein,5);
bilhete.BI=atoi(biBuf);
p+=5;
memcpy(bilhete.nome,p,51);
p+=51;
memcpy(bilhete.partida,p,11);
p+=11;
memcpy(bilhete.chegada,p, 11);
p+=11;
memcpy(dataBuf,p,2);
bilhete.data[0]=atoi(dataBuf);
printf("%.51s %d\n",bilhete.nome, bilhete.data[0]);
}
I am not sure what you wanted to do with the data variable but this will give you an integer. Observe, the biBuf and dataBuf variables are allocated in the loop so they get set to zeros each time.
Upvotes: 0