i'm starting in C programming and I have this issue in a school project. I have a text file with contracts that looks like this:
609140307 Carla Aguiar Cunha Paredes Pires PT 309 181 020 533 713 02F 13.8
814991297 Ricardo Andrade Nogueira Matos PT 099 597 635 807 514 05D 10.35
843818099 Eduardo Carneiro Paredes Clementino Castro PT 829 961 009 571 587 02D 5.75
647507641 Cristiana Eanes Almada Martins Baptista PT 257 687 479 093 378 02E 10.35
684741046 Marisa Calado Cardoso Quadros Barbosa PT 722 479 016 817 208 0RC 10.35
...
The fields are separated by a tab and it's around 10.000 lines of contracts
I need to store every line to a struct. This is what I've done:
#include <stdio.h>
typedef struct {
char id_contract[10];
char name[60];
char id_local[26];
char power[5];
}CONTRACTS;
void main() {
CONTRACTS c[10000] = { 0 };
int i = 0;
FILE *file = fopen("contracts.txt", "r");
if (file)
{
char line[120];
while (fgets(line, sizeof line, file) && i < 5)
{
if (sscanf(line, "%9s%60s%26s%5s",
c[i].id_contract,
c[i].name,
c[i].id_local,
c[i].power) == 4)
{
printf("Contract ID = %s\n", c[i].id_contract);
printf("Name = %s\n", c[i].name);
printf("Local ID = %s\n", c[i].id_local);
printf("Power = %s\n", c[i].power);
++i;
}
}
else {
printf("Error!\n");
}
}
And this is the output I get:
Contract ID = 609140307
Name = Carla
Local ID = Aguiar
Power = Cunha
Contract ID = 814991297
Name = Ricardo
Local ID = Andrade
Power = Nogue
Contract ID = 843818099
Name = Eduardo
Local ID = Carneiro
Power = Pared
So basically this is separating the fields by space and I don't know how to make it separate by a tab. I'm a beginner so it's difficult for me. Thank you in advance!