Foros del Web » Programación para mayores de 30 ;) » C/C++ »

Problema con manejo de archivos

Estas en el tema de Problema con manejo de archivos en el foro de C/C++ en Foros del Web. Tengo este código sencillo para manejo de archivos, pero no se porque tengo estos dos problemas: 1) cuando ejecuto y entro a inventario en la ...
  #1 (permalink)  
Antiguo 31/03/2013, 19:33
Avatar de kannonnr  
Fecha de Ingreso: junio-2011
Ubicación: Pasto
Mensajes: 39
Antigüedad: 12 años, 9 meses
Puntos: 1
Problema con manejo de archivos

Tengo este código sencillo para manejo de archivos, pero no se porque tengo estos dos problemas:

1) cuando ejecuto y entro a inventario en la parte de eliminar item de inventario, no elimina nada.
2) cuando ejecuto y entro a ventas, ingreso venta, y al momento de listar no lista nada.

Espero puedan ayudarme.

El código es el siguiente:

# include <conio.h>
# include <iostream.h>
# include <stdio.h>
# include <dos.h>
# include <ctype.h>

# define g gotoxy
# define p puts

FILE *fi;
FILE *fv;

void menu();
void main2();
void menu2();
void listarinventario();
void anadiritem();
void eliminaritem();
void main3();
void menu3();
void listarventas();
void anadirventas();
void eliminarventas();

struct inventario{
int cod;
char nombre[10];
int precio;
int estado;
};

struct ventas{
int codigo;
char cliente[10];
char producto[10];
int cantidad;
long total;
int estado;
};

void main(){
char op;
menu();

fi=fopen("c:/inventar.dat","a+");
fv=fopen("c:/ventas.dat","a+");

if(fi==NULL || fv==NULL)
{
g(15,15); p("Imposible crear archivos");
}
else
{
while(op!='3'){

g(44,24); op=getch();

switch(op){
case '1': main2();
break;

case '2': main3();
break;
}
clrscr();
menu();
}
}
}

//-------------- menu principal-----------
void menu(){
clrscr();
g(33,6); p("MENU PRINCIPAL");
g(28,11); p("1. Menu de inventario");
g(28,13); p("2. Menu de ventas");
g(28,15); p("3. Salir");
g(2,24); p(" Digite el numero de la opcion deseada: ");
}
//--------------main 2---------------------
void main2(){
char op;
menu2();

while(op!='4'){

g(44,24); op=getch();
switch(op){
case '1': listarinventario();
break;
case '2': anadiritem();
break;
case '3': eliminaritem();
break;
}
clrscr();
menu2();
}
}
//--------------menu 2---------------------
void menu2(){
clrscr();
g(31,6); p("MENU DE INVENTARIO");
g(29,11); p("1. Listar Inventario");
g(29,13); p("2. A¤adir Item");
g(29,15); p("3. Eliminar Item");
g(29,17); p("4. Regresar");
g(2,24); p(" Digite el numero de la opcion deseada: ");
}
//------listar inventario------------------
void listarinventario(){
clrscr();
struct inventario i;

rewind(fi);
g(25,2); p("LISTADO DE ITEMS EN EL INVENTARIO");

if(fi==NULL){
g(4,20); p("No se encontro el archivo");
getch();
}
else
{
int l,ff=6;

g(10,4); p("CODIGO");
g(30,4); p("NOMBRE");
g(50,4); p("PRECIO");

fseek(fi,0L,SEEK_END);
l=ftell(fi);
fseek(fi,0L,SEEK_SET);

for(int c=0;c<l;c+=sizeof(struct inventario))
{

fread(&i,sizeof(struct inventario),1,fi);

if(ff==18)
{
g(4,20); p("Pulse enter para continuar."); g(31,20);
for(int a=0;a<22;a++)
{
g(a+6,1); clreol();
}
ff=6;
}

if(i.estado==1)
{
g(10,ff); cout<<i.cod;
g(30,ff); cout<<i.nombre;
g(50,ff); cout<<i.precio;
ff++;
}
}
g(4,20); p("Pulse enter para salir.");
g(27,20); getch();
}
}
//------------a¤adir item------------------
void anadiritem(){

clrscr();
struct inventario i; int cod;

g(27,6); p("A¥ADIR NUEVO ITEM A INVENTARIO");
g(10,11); p("CODIGO:");
g(10,13); p("NOMBRE:");
g(10,15); p("PRECIO:");


rewind(fi);

g(18,11); cin>>cod;

i.cod=cod;
g(18,13); gets(i.nombre);
g(18,15); cin>>i.precio; i.estado=1;
fseek(fi,cod*sizeof(struct inventario),SEEK_SET);
fwrite(&i,sizeof(struct inventario),1,fi);
g(4,20); p("Item a¤adido correctamente.");
g(32,20);getch();
}
//-----------eliminar item------------------
void eliminaritem(){

clrscr();
struct inventario i;

rewind(fi);
g(34,2); p("ELIMINAR DE ITEM");

if(fi==NULL){
g(4,20); p("No se encontro el archivo");
getch();
}
else
{
int aux,l,se=0;
char op;

g(4,6); p("Codigo:");
g(12,6); cin>>aux;

fseek(fi,0L,SEEK_END);
l=ftell(fi);
fseek(fi,0L,SEEK_SET);

for(int c=0;c<l && se==0;c+=sizeof(struct inventario))
{

fread(&i,sizeof(struct inventario),1,fi);

if(i.cod==aux && i.estado==1)
{
se=1;
g(4,20); p("Esta seguro de eliminar este item?(S/N)");
g(44,20);op=toupper(getch());
if(op=='S')
{
fseek(fi,c,SEEK_SET);
i.estado=0;
fwrite(&i,sizeof(struct inventario),1,fi);
g(4,20); clreol();
g(4,20); p("Item eliminado.");
g(20,20);
}
else
{
g(4,20); clreol();
g(4,20); p("Item no eliminado.");
}
}
}
if(se==0)
{
g(4,20); clreol();
g(4,20); p("Item no encontrado.");
g(24,20);
}
}
getch();
}
//--------------main 3---------------------
void main3(){
char op;
menu3();

while(op!='4'){

g(44,24); op=getch();
switch(op){
case '1': listarventas();
break;
case '2': anadirventas();
break;
case '3': eliminarventas();
break;
}
clrscr();
menu3();
}
}
//--------------menu 3---------------------
void menu3(){
clrscr();
g(32,6); p("MENU DE VENTAS");
g(30,11); p("1. Listar Ventas");
g(30,13); p("2. A¤adir Venta");
g(30,15); p("3. Eliminar Venta");
g(30,17); p("4. Regresar");
g(2,24); p(" Digite el numero de la opcion deseada: ");
}
//------listar ventas------------------
void listarventas(){
clrscr();
struct ventas v;

rewind(fv);
g(33,2); p("LISTADO DE VENTAS");

if(fv==NULL){
g(4,20); p("No se encontro el archivo");
getch();
}
else
{
int l,ff=6;

g(10,4); p("CODIGO");
g(20,4); p("CLIENTE");
g(30,4); p("ITEM");
g(40,4); p("CANT");
g(50,4); p("TOTAL");

fseek(fv,0L,SEEK_END);
l=ftell(fv);
fseek(fv,0L,SEEK_SET);

for(int c=0;c<l;c+=sizeof(struct ventas))
{

fread(&v,sizeof(struct inventario),1,fv);

if(ff==18)
{
g(4,20); p("Pulse enter para continuar."); g(31,20);
for(int a=0;a<22;a++)
{
g(a+6,1); clreol();
}
ff=6;
}

if(v.estado==1)
{
g(10,ff); cout<<v.codigo;
g(20,ff); cout<<v.cliente;
g(30,ff); cout<<v.producto;
g(40,ff); cout<<v.cantidad;
g(50,ff); cout<<v.total;

ff++;
}
}
g(4,20); p("Pulse enter para salir.");
g(27,20); getch();
}
}
//------------a¤adir venta-----------------
void anadirventas(){

clrscr();
struct ventas v; int cod;

g(31,6); p("A¥ADIR NUEVA VENTA");
g(10,11); p("CODIGO :");
g(10,13); p("CLIENTE :");
g(10,15); p("ITEM :");
g(10,17); p("CANTIDAD:");
g(10,19); p("TOTAL :");


rewind(fv);

g(22,11); cin>>cod;

v.codigo=cod;
g(22,13); gets(v.cliente);
g(22,15); gets(v.producto);
g(22,17); cin>>v.cantidad;
g(22,19); cin>>v.total;
v.estado=1;
fwrite(&v,sizeof(struct ventas),1,fv);
g(4,20); p("Item a¤adido correctamente.");
g(32,20);getch();
}
//-----------eliminar ventas------------------
void eliminarventas(){

clrscr();
struct ventas v;

rewind(fv);
g(34,2); p("ELIMINAR DE ITEM");

if(fv==NULL){
g(4,20); p("No se encontro el archivo");
getch();
}
else
{
int aux,l,se=0;
char op;

g(4,6); p("Codigo:");
g(12,6); cin>>aux;

fseek(fv,0L,SEEK_END);
l=ftell(fv);
fseek(fv,0L,SEEK_SET);

for(int c=0;c<l && se==0;c+=sizeof(struct ventas))
{

fread(&v,sizeof(struct ventas),1,fv);

if(v.codigo==aux && v.estado==1)
{
se=1;
g(4,20); p("Esta seguro de eliminar este item?(S/N)");
g(44,20);op=toupper(getch());
if(op=='S')
{
fseek(fv,c,SEEK_SET);
v.estado=0;
fwrite(&v,sizeof(struct ventas),1,fv);
g(4,20); clreol();
g(4,20); p("Item eliminado.");
g(20,20);
}
else
{
g(4,20); clreol();
g(4,20); p("Item no eliminado.");
}
}
}
if(se==0)
{
g(4,20); clreol();
g(4,20); p("Item no encontrado.");
g(24,20);
}
}
getch();
}
__________________
Bosatzu Kannon
Keep Moving Forward

Etiquetas: int, manejo, salir
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 22:26.