No he podido responderte antes. Prueba este codigo a ver si te sirve:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stRegistro {
int ID;
char nombre[25];
int cantidad;
};
int Menu(void);
void ChangeStock(char *operacion, struct stRegistro *reg);
void MostrarCabecera(void);
void MostrarProducto(long n, struct stRegistro *reg);
long LeeNumero(void);
void Borrar(FILE **fa, long numero);
int Size(FILE **fa, int tam);
//--------------------------------------------------------------------------
int main()
{
struct stRegistro reg;
struct stRegistro aux;
char nombre[25];
FILE *fa;
int i, opcion, nRegistros, encontrado, ID;
long numero;
fa
= fopen("stock.dat", "r+b"); // Este modo permite leer y escribir if(!fa
) fa
= fopen("stock.dat", "w+b"); // si el fichero no existe, lo crea. do {
opcion = Menu();
switch(opcion) {
case '1': // Añadir o incrementar el stock
encontrado = 0;
ChangeStock("Añadir al stock", &aux);
nRegistros = Size(&fa, sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®
,sizeof(struct stRegistro
),1,fa
); if(strcmp(aux.
nombre, reg.
nombre) == 0) {
reg.cantidad += aux.cantidad;
fseek(fa
,i
*sizeof(struct stRegistro
),SEEK_SET
); fwrite(®
, sizeof(struct stRegistro
), 1, fa
); encontrado = 1;
break;
}
}
if(encontrado == 0)
{
if(nRegistros == 0)
ID = 0;
else
{
fseek(fa
, 0, nRegistros
); fread(®
,sizeof(struct stRegistro
), 1, fa
); ID = reg.ID + 1;
}
aux.ID = ID;
fwrite(&aux
, sizeof(struct stRegistro
), 1, fa
); }
break;
case '2': // Ventas de stockaje
encontrado = 0;
ChangeStock("Venta de stockaje", &aux);
nRegistros = Size(&fa, sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®
,sizeof(struct stRegistro
),1,fa
); if(strcmp(aux.
nombre, reg.
nombre) == 0) {
encontrado = 1;
if(aux.cantidad <= reg.cantidad)
{
reg.cantidad -= aux.cantidad;
fseek(fa
,i
*sizeof(struct stRegistro
),SEEK_SET
); fwrite(®
, sizeof(struct stRegistro
), 1, fa
); }else{
printf("No hay suficiente stockaje de ese producto para esta venta.\n"); MostrarCabecera();
MostrarProducto(i, ®);
printf("|---------|-------------------------|--------|\n\n"); }
break;
}
}
if(encontrado == 0)
{
printf("No se puede encontrar el producto indicado.\n\n"); }
break;
case '3': // Mostrar stock de un producto por su ID
encontrado = 0;
printf("Buscar stock por ID: "); ID = LeeNumero();
nRegistros = Size(&fa, sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®
,sizeof(struct stRegistro
),1,fa
); if(ID == reg.ID)
{
encontrado = 1;
MostrarCabecera();
MostrarProducto(i, ®);
break;
}
}
if(encontrado == 0)
{
printf("No se puede encontrar el producto indicado.\n\n"); }
else
{
printf("|---------|-------------------------|--------|\n\n"); }
break;
case '4': // Mostrar stock de un producto por su nombre
encontrado = 0;
printf("Buscar stock por nombre: "); for(i
= strlen(nombre
)-1; i
&& nombre
[i
] < ' '; i
--) nombre[i] = 0;
nRegistros = Size(&fa, sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®
,sizeof(struct stRegistro
),1,fa
); if(strcmp(nombre
, reg.
nombre) == 0) {
encontrado = 1;
MostrarCabecera();
MostrarProducto(i, ®);
break;
}
}
if(encontrado == 0)
{
printf("No se puede encontrar el producto indicado.\n\n"); }
else
{
printf("|---------|-------------------------|--------|\n\n"); }
break;
case '5': // Mostrar todo el stockaje
encontrado = 0;
numero = 0;
nRegistros = Size(&fa,sizeof(struct stRegistro));
if(nRegistros > 0)
{
encontrado = 1;
MostrarCabecera();
for(i = 0; i < nRegistros; i++)
{
fread(®
, sizeof(struct stRegistro
), 1, fa
); MostrarProducto(numero++, ®);
}
}
if(encontrado == 0)
{
printf("No existen entradas en el registro.\n"); }
else
{
printf("|---------|-------------------------|--------|\n\n"); }
break;
case '6': // Eliminar del stock
printf("Eliminar del stock: "); numero = LeeNumero();
Borrar(&fa, numero);
break;
}
} while(opcion != '0');
return 0;
}
//--------------------------------------------------------------------------
// Muestra un menú con las opciones disponibles y captura una opción del usuario
int Menu()
{
char resp[20];
do {
printf("1- Añadir al stock\n"); printf("3- Buscar stock por su ID\n"); printf("4- Buscar stock por su nombre\n"); printf("5- Mostrar todo el stockaje\n"); printf("6- Eliminar producto del stock por su ID\n"); } while(resp[0] < '0' && resp[0] > '6');
return resp[0];
}
//--------------------------------------------------------------------------
// Permite que el usuario introduzca un producto por pantalla
void ChangeStock(char * operacion, struct stRegistro *reg)
{
int i;
char numero[6];
fgets(reg
->nombre
, 25, stdin
); // la función fgets captura el retorno de línea, hay que eliminarlo:
for(i
= strlen(reg
->nombre
)-1; i
&& reg
->nombre
[i
] < ' '; i
--) reg->nombre[i] = 0;
reg
->cantidad
= atoi(numero
);}
//--------------------------------------------------------------------------
// Muestra la cabecera de la tabla por pantalla
void MostrarCabecera(void)
{
printf("|---------|-------------------------|--------|\n" "|ID |Nombre |Cantidad|\n"
"|---------|-------------------------|--------|\n");
}
//--------------------------------------------------------------------------
// Muestra un producto por pantalla
void MostrarProducto(long n, struct stRegistro *reg)
{
printf("|[%6ld] |%-25s| %4d |\n", reg
->ID
, reg
->nombre
, reg
->cantidad
); }
//--------------------------------------------------------------------------
// Lee un número suministrado por el usuario
long LeeNumero()
{
char numero[6];
}
//--------------------------------------------------------------------------
// Borra una entrada del archivo
void Borrar(FILE **fa, long numero)
{
struct stRegistro reg;
FILE *ftemp;
int largo, nRegistros, i;
ftemp
= fopen("temp.dat", "wb"); nRegistros = Size(*&fa,sizeof(struct stRegistro));
for(i=0; i<nRegistros; i++)
{
fread(®
,sizeof(struct stRegistro
),1,*fa
); if(numero != reg.ID)
{
fwrite(®
, sizeof(struct stRegistro
), 1, ftemp
); }
}
rename("temp.dat", "stock.dat"); *fa
= fopen("stock.dat", "r+b"); }
//--------------------------------------------------------------------------
int Size(FILE **fa, int tam)
{
int largo, n;
n = largo / tam;
return n;
}
//--------------------------------------------------------------------------