Eso y el gets por fgets para prevenir desbordamiento de buffer. Aquí el código modificado:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct materiales{
int codart, stock, stockmin;
char desc[20];
float precio;
};
struct ventas{
int cod,cant;
};
//ACA YO CARGO EL ARCHIVO AMTERIALES
int main() {
FILE *archivomateriales;
FILE *archivoventa;
int op;
struct materiales mat;
struct ventas ven;
if((archivomateriales
=fopen("materiales.dat","wb")) != NULL
) { do {
printf("\n\tIngrese el codigo del articulo"); scanf("%d", &mat.
codart); printf("\n\tIngrese un breve descripcion"); fgets(mat.
desc, 20, stdin
); printf("\n\tIngrese stock actual del articulo"); printf("\n\tIngrese stock minimo"); scanf("%d", &mat.
stockmin); printf("\n\tPor utimo ingrese precio"); scanf("%f", &mat.
precio); fwrite(&mat
, sizeof(mat
), 1, archivomateriales
); printf("\n\t¿Desea ingresar otro articulo? [1]-Para SI // [0]-Para NO"); } while(op != 0);
}
else {
printf("\n\nError de archivo"); }
//ACA YO CARGO EL ARCHIVO VENTAS
int opc;
if((archivoventa
=fopen("ventas.dat","wb")) != NULL
) { do{
printf("\n\tIngrese codigo del articulo"); printf("\n\tIngrese cantidad vendida del articulo"); fwrite(&ven
,sizeof(ven
),1,archivoventa
); printf("\n\t¿Desea registrar otra venta? [1]-Para SI // [0]-Para NO"); } while(opc!=0);
}
else {
}
//ACA ES DONDE ESTA LA FALLA
if((archivoventa
=fopen("ventas.dat","rb")) != NULL
) { if((archivomateriales
=fopen("materiales.dat","r+b")) != NULL
) { fread(&ven
, sizeof(ven
), 1, archivoventa
); while(!feof(archivoventa
)) { fread(&mat
, sizeof(mat
), 1, archivomateriales
); while(!feof(archivomateriales
)) { if(mat.codart == ven.cod) {
mat.stock = mat.stock-ven.cant;
printf("\n\nEl stock entonces quedo en %d", mat.
stock); fseek(archivomateriales
, -1*(sizeof(mat
)),SEEK_CUR
); fwrite(&mat
,sizeof(mat
), 1, archivomateriales
); }
fread(&mat
, sizeof(mat
), 1, archivomateriales
); }
fread(&ven
,sizeof(ven
),1,archivoventa
); }
}
else {
}
}
else {
}
if((archivomateriales
=fopen("materiales.dat","rb")) != NULL
){ fread(&mat
, sizeof(mat
), 1, archivomateriales
); while(!feof(archivomateriales
)) { printf("\n\nEl codigo de articulo es %d", mat.
codart); printf("\n\nEl stock actualizado quedo asi %d", mat.
stock); fread(&mat
, sizeof(mat
), 1, archivomateriales
); }
}
else {
}
}