0
I have a txt file that follows the template below. I want to save the balance as int, but with fgets I can’t get it. I’m wanting to save as int, because after the data is taken the program sorts them based on the balance. Is there any other way to do this???
nome
conta
saldo
Kelsen da Silva
54432
3456
William Malvezzi
67890
154998
Fulano de Tal
14441
23987
The code that gave to take the data and print is like this.
struct conta{
char nome[100];
char conta[4];
char saldo[10];
};
int main(){
int i = 0;
struct conta contas[10];
FILE *file = fopen("string.txt", "r");
while(!feof(file)){
fgets(contas[i].nome, sizeof(contas[i].nome), file);
fgets(contas[i].conta, sizeof(contas[i].conta), file);
fgets(contas[i].saldo, sizeof(contas[i].saldo), file);
i++;
};
fclose(file);
}
A detail to note: a string in C is a string followed by the terminator character ' 0'. Like this
char conta[4];
can store a string of up to 3 characters (plus terminator ' 0'). By your data (e.g.: 54432) you would need to declarechar conta[6];
or more if the account has more digits.– anonimo