#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define TAM 30
 
void visualizar_variable ();
int nueva_variable (struct variable nvariable [], int cantidad);
 
struct variable
{
char novela[TAM],ensayo[TAM],libro[TAM], nnovela[TAM],ntnovela[TAM];
char nensayo[TAM],ntensayo[TAM],nlibro[TAM],ntlibro[TAM];
};
 
main()
{
   char salir[5],aux;
   struct variable nvariable [TAM];
   int fin,opcion,cantidad=0,conta=0,contb=0;
 
   do{
      printf("Elije una opcion:nn");
      printf("1. Introducir datos (numero de titulos y numero de ejemplares por tema).n");
      printf("2. Visualizar datos y numero total de libros disponibles en la libreria.n");
      printf("3. Guardar los datos en un fichero de texto que se llamara libros.txtn");
      printf("4. Salir.n");
      printf("nSu opcion:n");
      opcion=getche();
      system("CLS");
 
      switch(opcion)
      {
         case'1':
            conta++;
            cantidad = nueva_variable (nvariable, cantidad);
            break;
         case'2':
            if((conta!=0)&&(contb!=0))
            {
            visualizar_variable ();
            }
            else
            {printf("No hay datos guardadosn");}
            break;
         case'3':
         contb++;
         printf("Datos Guardadosn");
            FILE * fichero_ptr;
            int i;
            fichero_ptr = fopen("libros.txt","a+");
            /* Colocamos el cursor al final del archivo para añadir un
               nuevo registro */
            fseek(fichero_ptr,0,SEEK_END);
            for( i=0; i< cantidad; i++)
               fwrite(&nvariable[i],sizeof(struct variable),1,fichero_ptr);
            fclose(fichero_ptr);
            break;
         case'4':
            return 0;
            break;
         default:
            printf("Opcion incorrectan");
      }
      printf("nnDeseas continuar? ");
      fflush(stdout);
      scanf("%4s",salir);
      if(strcmp(salir,"si")==0 || strcmp(salir,"SI")==0 || strcmp(salir,"Si")==0)
      {
         fin=1;
         system("CLS");
      }
      else
         fin=0;
   }while(fin==1);
   return 0;
}
 
/*Esta funcion guarda las variables*/
int nueva_variable(struct variable variable_ [], int cuantos)
{
   int donde = cuantos;
   if (cuantos<TAM)
   {
      printf ("nIntroduce la palabra 'Novela': ");
      /*Realizamos el “fflush (stdin)” para limpiar el buffer de entrada*/
      fflush (stdin);
      gets (variable_ [donde].novela);
      printf ("Numero de titulos: ");
      fflush (stdin);
      scanf ("%s", & variable_ [donde].nnovela);
      printf ("Numero total de ejemplares: ");
      fflush (stdin);
      scanf ("%s", & variable_ [donde].ntnovela);
      printf ("nIntroduce la palabra 'Ensayo':");
      fflush (stdin);
      gets (variable_ [donde].ensayo);
      printf ("Numero de titulos: ");
      fflush (stdin);
      scanf ("%s", & variable_ [donde].nensayo);
      printf ("Numero total de ejemplares: ");
      fflush (stdin);
      scanf ("%s", & variable_ [donde].ntensayo);
      printf ("nIntroduce la palabra 'Libro de texto':");
      fflush (stdin);
      gets (variable_ [donde].libro);
      printf ("Numero de titulos: ");
      fflush (stdin);
      scanf ("%s", & variable_ [donde].nlibro);
      printf ("Numero total de ejemplares: ");
      fflush (stdin);
      scanf ("%s", & variable_ [donde].ntlibro);
      cuantos++;
   }
   else
   {
      printf ("No tengo más espacio");
   }
   return cuantos;
}
 
/*Esta funcion visualiza todas las variable*/
void visualizar_variable ()
{
   FILE*fichero_ptr;
   char carac;
   int contador;
 
/* Usamos una estructura auxiliar para recuperar registro por
   registro del archivo */
   struct aux
   {
   char novela[TAM],ensayo[TAM],libro[TAM], nnovela[TAM],ntnovela[TAM];
   char nensayo[TAM],ntensayo[TAM],nlibro[TAM],ntlibro[TAM];
   }auxiliar;
 
   if((fichero_ptr=fopen("libros.txt","a+t"))!=NULL)
   {
      fseek(fichero_ptr,0,SEEK_SET); //Colocamos el cursor al inicio del archivo
      contador=1;
 
      while (fread(&auxiliar,sizeof(auxiliar),1,fichero_ptr))
      {
         printf("Registro: %dn-------------n",contador);
         printf("%sn", auxiliar.novela);
         printf("Numero de titulos: %sn", auxiliar.nnovela);
         printf("Numero total de ejemplares: %sn", auxiliar.ntnovela);
         printf("%sn", auxiliar.ensayo);
         printf("Numero de titulos: %sn", auxiliar.nensayo);
         printf("Numero total de ejemplares: %sn", auxiliar.ntensayo);
         printf("%sn", auxiliar.libro);
         printf("Numero de titulos: %sn", auxiliar.nlibro);
         printf("Numero total de ejemplares: %snn", auxiliar.ntlibro);
         contador++;
      }
      fclose(fichero_ptr);
   }
   else
   {
      printf("nNo hay datosn");
   }
}