Está en la página 1de 2

//Programa que crea un archive, ingresa datos y visualiza datos

// Realizado por Inga. Susana Beltrán


#include <stdio.h>
#include <conio>
struct sRegistro {
char producto[25];
int codigo;
float precio;
} registro;

void menu()
{ clrscr();
gotoxy(25,8); printf(" Menu:");
gotoxy(25,9); printf(" 1. Crear fichero");
gotoxy(25,10); printf(" 2. Insertar datos");
gotoxy(25,11); printf(" 3. Ver datos");
gotoxy(25,12); printf(" 4. Salir");
}

void CrearFichero(FILE *Fichero)


{ Fichero = fopen("fichero", "r");
if(!Fichero)
{ Fichero = fopen("fichero", "w");
printf("\nArchivo creado!");
}
else
{ printf("\nEl fichero ya existe!");
}
fclose (Fichero);
}
void InsertarDatos(FILE *Fichero)
{ clrscr();
Fichero = fopen("fichero", "a+");

if(Fichero == NULL)
{
printf("\nFichero no existe! \nPor favor creelo");
return;
}

gotoxy(10,8); printf("nombre producto: ");


textcolor(10);
gotoxy(30,8); cprintf("ùùùùùùùùùùùùùùù");
gotoxy(30,8);scanf("%s", &registro.producto);
gotoxy(10,10); printf("Código producto: ");
scanf("%d", &registro.codigo);
gotoxy(10,12); printf("Precio producto: ");
scanf("%f", &registro.precio);
fwrite(&registro, sizeof(struct sRegistro), 1, Fichero);
fclose(Fichero);
}
void enca()
{ gotoxy(10,2);cprintf("Numero");
gotoxy(20,2);cprintf("Codigo");
gotoxy(35,2);cprintf("Nombre");
gotoxy(45,2);cprintf("Precio");
}
void VerDatos(FILE *Fichero)
{ clrscr();
enca();
int numero = 1;
Fichero = fopen("fichero", "r");
if(Fichero == NULL)
{
printf("\nFichero no existe! \nPor favor creelo");
return;
}

fread(&registro, sizeof(struct sRegistro), 1, Fichero);


while(!feof(Fichero))
{
gotoxy(10,numero+3); printf("\%d ", numero);
gotoxy(20,numero+3);printf("\%d ", registro.codigo);
gotoxy(35,numero+3);printf("\%s " , registro.producto);
gotoxy(45,numero+3);printf("\%.2f",registro.precio);
fread(&registro, sizeof(struct sRegistro), 1, Fichero);
numero++;
}
getch();
fclose(Fichero);
}
void main()
{ int opcion;
int exit = 0;
FILE *fichero;
while (!exit)
{ menu();
gotoxy(25,13);printf("Opcion: ");
scanf("%d", &opcion);
switch(opcion)
{ case 1: CrearFichero(fichero); break;
case 2: InsertarDatos(fichero); break;
case 3: VerDatos(fichero); break;
case 4: exit = 1; break;
default: printf("\nopcion no valida");
}
}
}

También podría gustarte