Hi, I have a little doubt, how to use the fseek function in c to search for a specific type of data. I've been searching but no answer gives me an idea of how I should use the function, (I'm using dev-c) I leave the code that I have:
#include < stdio.h >
#include < string.h >
struct dClientes {
char nombreCompleto[40];
};
struct cedula {
int id, cuenta;
};
struct saldos {
float saldoAnterior, saldoActual;
};
struct banco {
cedula s;
saldos f;
dClientes DC;
};
int leer();
void llenaClientes(banco);
FILE *abrirArchivo();
void muestraArchivo(banco);
void muestraCuentas(banco);
int main() {
int opc;
banco clientes;
do {
printf("\nOpcion: ");
opc = leer();
switch (opc) {
case 1:
llenaClientes(clientes);
break;
case 2:
muestraArchivo(clientes);
break;
case 3:
muestraCuentas(clientes);
break;
}
} while (opc != 0);
}
int leer() {
int n;
scanf("%i", &n);
return n;
}
void muestraCuentas(banco l) {
char cadena[sizeof(banco)], *mind;
FILE *apufile = abrirArchivo();
if (apufile == NULL) {
printf("\nERROR, NO SE ENCONTRO ARCHIVO...");
} else {
while (!feof(apufile)) {
/*aqui es donde quiero usar fseek para encontrar el listado de cuentas con
* su respectivo saldo...*/
}
fclose(apufile);
}
}
void muestraArchivo(banco l) {
printf(">>>DATOS DE LOS CLIENTES<<<");
char cadena[sizeof(banco)], *token;
FILE *apufile = abrirArchivo();
if (apufile == NULL) {
printf("\nERROR, NO SE ENCONTRO ARCHIVO...");
} else {
while (!feof(apufile)) {
fgets(cadena, sizeof(banco), apufile);
if (!feof(apufile)) {
token = NULL;
token = strtok(cadena, "|");
printf("\n Numero de cuenta: %s ", token);
token = strtok(NULL, "|");
printf("\n Cedula: %s", token);
token = strtok(NULL, "|");
printf("\n Saldo actual del cliente: %s", token);
token = strtok(NULL, "|");
printf("\n Nombre del cliente: %s", token);
}
printf("\n");
}
fclose(apufile);
}
}
void llenaClientes(banco l) {
printf("\tBienvenido al sistema de Banamex.\n\t>>>Por favor a continuacion "
"capture sus datos<<<");
FILE *apuB;
fflush(stdin);
printf("\nIngrese nombre completo del cliente: ");
gets(l.DC.nombreCompleto);
printf("\nIngrese el numero de cuenta: ");
l.s.cuenta = leer();
printf("\nIngrese el numero de cedula: ");
l.s.id = leer();
printf("\nIngrese el saldo del cliente: ");
scanf("%d", &l.f.saldoActual);
l.f.saldoAnterior = l.f.saldoActual;
apuB = abrirArchivo();
if (apuB == NULL) {
printf("\nNo se pudo abrir el archivo...");
} else {
fprintf(apuB, "%i|%i|%f|%s|\n", l.s.id, l.s.cuenta, l.f.saldoActual,
l.DC.nombreCompleto);
fclose(apuB);
}
}
FILE *abrirArchivo() {
FILE *apufile = fopen("Banamex.txt", "a+");
return apufile;
}