1. #include <conio.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5.  
  6. struct productos
  7. {
  8.     int codigo;
  9.     char nombre[50];
  10.     int existencia;
  11.     float precio;
  12. };
  13.  
  14. struct nodo
  15. {
  16.     struct productos dato;
  17.     struct nodo *proximo;
  18. };
  19.  
  20. /* Declaracion de funciones */
  21. void archivo(FILE *fp);
  22. struct nodo *nuevonodo();
  23. struct nodo *creapila(struct nodo *pri, struct productos x);
  24. void muestra(struct nodo *pri, FILE *fp);
  25. /* Fin de Declaracion */
  26.  
  27. void main()
  28. {
  29.     struct productos x;
  30.     struct nodo *pri=NULL;
  31.     FILE *fp;
  32.     char opcion; float auxiliar=0;
  33.     if((fp=fopen("C:\\Datos.txt","wb"))==NULL)
  34.     {
  35.         getch();
  36.     }
  37.     fseek(fp,0,2);
  38.     do
  39.     {
  40.         fflush(stdin);
  41.         printf("Ingrese el Codigo de Producto ");
  42.         scanf("%d",&x.codigo);
  43.         fflush(stdin);
  44.         printf("Ingrese Nombre de Producto ");
  45.         gets(x.nombre);
  46.         fflush(stdin);
  47.         printf("Ingrese la Existencia ");
  48.         scanf("%d",&x.existencia);
  49.         fflush(stdin);
  50.         printf("Ingrese el Precio ");
  51.         scanf("%f",&auxiliar); x.precio=auxiliar;
  52.         pri=creapila(pri,x);
  53.         fflush(stdin);
  54.         printf("Desea Ingresar otro Registro? (S/N) ");
  55.         scanf("%c",&opcion); opcion=toupper(opcion);
  56.     } while(opcion=='S');
  57.     muestra(pri,fp);
  58.     fflush(stdin);
  59.     printf("El contenido de la Pila se ha Guardado. Desea Visualizarlo? (S/N)");
  60.     scanf("%c",&opcion); opcion=toupper(opcion);
  61.     if(opcion=='S') archivo(fp);
  62.     getch();
  63.     fclose(fp);
  64. }
  65.  
  66. struct nodo *creapila(struct nodo *pri, struct productos x)
  67. {
  68.     struct nodo *p;
  69.     p=nuevonodo();
  70.     (*p).dato=x;
  71.     (*p).proximo=pri;
  72.     return p;
  73. }
  74.  
  75. struct nodo *nuevonodo()
  76. {
  77.     struct nodo *p;
  78.     p=(struct nodo *)malloc(sizeof(struct nodo));
  79.     if(p==NULL)
  80.     {
  81.         printf("Memoria RAM Llena");
  82.         getch();
  83.         exit(0);
  84.     }
  85.     return p;
  86. }
  87.  
  88. void muestra(struct nodo *pri, FILE *fp)
  89. {
  90.     clrscr();
  91.     struct nodo *aux;
  92.     while(pri!=NULL)
  93.     {
  94.         printf("Codigo: %d \n",(*pri).dato.codigo);
  95.         printf("Nombre: %s \n",(*pri).dato.nombre);
  96.         printf("Existencia: %d \n",(*pri).dato.existencia);
  97.         printf("Precio: %0.2f \n\n",(*pri).dato.precio);
  98.         fwrite(&pri->dato,sizeof(struct productos),1,fp);
  99.         aux=pri;
  100.         pri=(*pri).proximo;
  101.         free(aux);
  102.     }
  103. }
  104.  
  105. void archivo(FILE *fp)
  106. {
  107.     struct productos x;
  108.     clrscr();
  109.     printf("Datos del Archivo:\n\n");
  110.     fread(&x,sizeof(struct productos),1,fp);
  111.     while(!feof(fp))
  112.     {
  113.         printf("Codigo: %d \n",x.codigo);
  114.         printf("Nombre: %s \n",x.nombre);
  115.         printf("Existencia: %d \n",x.existencia);
  116.         printf("Precio: %0.2f \n\n",x.precio);
  117.         fread(&x,sizeof(struct productos),1,fp);
  118.     }
  119.     printf("Fin de Archivo");
  120. }