• Lunes 29 de Abril de 2024, 04:31

Autor Tema:  Como Usar Fread() Y Fwrite() Usando Pilas ?  (Leído 2657 veces)

pabju

  • Miembro activo
  • **
  • Mensajes: 34
    • Ver Perfil
Como Usar Fread() Y Fwrite() Usando Pilas ?
« en: Miércoles 7 de Enero de 2004, 02:57 »
0
holas usuarios, me pueden explicar en codigo fuente de como usar fread(), frwite()
usando pilas, tendiendo la estructura de datos serian:

struct TReg
{
     int vID;
    char vName[20];
    struct TReg *next;
};

//----------------------------------------------
// DECLARACION GLOBAL
//----------------------------------------------
TReg *main,*prim,*ultimo,*aux=NULL;

//----------------------------------------------

para guardar los datos:

        if (main==NULL) return;
        aux=main;
        FILE *fichero= fopen("C:\\file.rec","wb");
        while (aux!=NULL)
        {
           aux->vID = main->vID;
           strcpy(aux->vName,main->vName);
           fwrite(( char *)&aux,sizeof(aux),1,fichero);        
           aux=aux->Next;
        }
        fclose(fichero);


para leer los datos
   struct TReg *nuevo, *anterior,*aux, *primer=NULL;
   FILE *fichero= fopen("C:\\file.rec","rb");        
   fread(( char *)&aux,sizeof(aux),1,fichero);
   while (!feof(fichero))
   {          
        nuevo=(struct TReg*)calloc(1,sizeof(TReg));
        nuevo->vID = aux->vID;
        strcpy(nuevo->vName,aux->vName);
        Mostrar(nuevo->vID,nuevo->vName);  //Mostraran los datos en la pantalla
        nuevo->Next = NULL;
        if(primer==NULL)
            primer=nuevo;
        else
            anterior->Next =nuevo;
           
        anterior=nuevo;
       

        fread(( char *)&aux,sizeof(aux),1,fichero);
   }
   fclose(fichero);


Espero que me den las respuestas, muchas gracias desde ya.

Pablo

Martin Candurra

  • Miembro activo
  • **
  • Mensajes: 36
    • Ver Perfil
Re: Como Usar Fread() Y Fwrite() Usando Pilas ?
« Respuesta #1 en: Miércoles 7 de Enero de 2004, 23:06 »
0
A que te referís con usando pilas ?
No estarás confunciendo pila con lista ?

pabju

  • Miembro activo
  • **
  • Mensajes: 34
    • Ver Perfil
Re: Como Usar Fread() Y Fwrite() Usando Pilas ?
« Respuesta #2 en: Jueves 8 de Enero de 2004, 02:36 »
0
perdone, no se como usar con la lista, me podrias explicar (en codigo fuente)...

Martin Candurra

  • Miembro activo
  • **
  • Mensajes: 36
    • Ver Perfil
Re: Como Usar Fread() Y Fwrite() Usando Pilas ?
« Respuesta #3 en: Jueves 8 de Enero de 2004, 16:59 »
0
Fijate si estos links te sirven :-)
http://www.ucm.es/info/dsip/clavel/courses...03/node176.html
http://libros.es.gnome.org/librognome/libr...nome/x2087.html

A veces se gana más tiempo buscando en Google.

Saludos

pabju

  • Miembro activo
  • **
  • Mensajes: 34
    • Ver Perfil
Re: Como Usar Fread() Y Fwrite() Usando Pilas ?
« Respuesta #4 en: Viernes 9 de Enero de 2004, 03:18 »
0
gracias..pero tambien necesito informacion o codigo fuente sobre  lista + archivos ( fread() y fwrite() ) todos juntos..la manera de guardar los datos de lista por medio de un archivo...

muchas gracias

dreadlock

  • Miembro activo
  • **
  • Mensajes: 66
    • Ver Perfil
Re: Como Usar Fread() Y Fwrite() Usando Pilas ?
« Respuesta #5 en: Martes 13 de Enero de 2004, 05:21 »
0
Perdon borre esto por que descubri el "modificar"
UN intento de explicar lo inezplicable:D

dreadlock

  • Miembro activo
  • **
  • Mensajes: 66
    • Ver Perfil
Re: Como Usar Fread() Y Fwrite() Usando Pilas ?
« Respuesta #6 en: Martes 13 de Enero de 2004, 05:23 »
0
Pues creo que te entendi tu quieres que se guarde el contenido de una lista a un archivo. Como hay dos tipos de listas enlazadas(simplemente enlazadas y doblemente enlazadas), yo te pongo la que ocupa fread, y fwrite, esque la de doblemente enlazada en mi version personal lee bitexbite.
Código: Text
  1.  
  2. //Natty Dread Lock
  3. //lista simplemente enlazada
  4. #include<stdio.h>
  5. #include<string.h>
  6. #include<conio.h>
  7. #include<ctype.h>
  8. #include<dos.h>
  9. #include<stdlib.h>
  10. #include<a:portada.h>
  11. # define MAX 6
  12. int i;
  13. int borrando=0;
  14. void intro_ficha(void);
  15. void lista_ficha(void);
  16. void muestra_ficha(void);
  17. void borra_ficha(void);
  18. void almacena_datos(void);
  19. void lee_datos(void);
  20. void asigna_memoria(void);
  21. void visu_ficha(struct agenda*);////////////////////////
  22. struct agenda *display_ficha(char *n);
  23. struct agenda *busca(char *inbuf);
  24. struct agenda *primero,*nuevo,*indice;
  25. //////////////////////////////////
  26. struct agenda{
  27. char nombre[30];
  28. char direccion[50];
  29. char telefono [10];
  30. struct agenda *siguiente;//apunta al siguiente elemento de la lista
  31. };
  32. //////////////////////////////////
  33. void asigna_memoria(){
  34. nuevo=(struct agenda*)malloc(sizeof(struct agenda));
  35. if(nuevo==NULL){
  36. gotoxy(2,13);printf("Espacio de memoria insuficiente");
  37. getche();
  38. for(i=2;i<=79;i++){
  39. gotoxy(i,13);printf(" ");}
  40. return;
  41. }
  42. if(primero==(struct agenda*)NULL)
  43. primero=indice=nuevo;
  44. else{
  45. indice=primero;
  46. while(indice->siguiente!=(struct agenda*)NULL)
  47. indice=indice->siguiente;
  48. indice->siguiente=nuevo;
  49. indice=nuevo;}}
  50. /////////////////////////////////////////////////////
  51. void intro_ficha(){
  52. asigna_memoria();
  53. for(i=2;i<=79;i++){
  54. gotoxy(i,13);printf(" ");}
  55. gotoxy((80-19)/2,13);printf("Agregando Datos!!!!");
  56. gotoxy(5,16);printf("Nombre: ");
  57. fflush(stdin);
  58. gets(indice->nombre);
  59. gotoxy(5,17);printf("Direccion: ");
  60. fflush(stdin);
  61. gets(indice->direccion);
  62. gotoxy(5,18);printf("Telefono: ");
  63. fflush(stdin);
  64. gets(indice->telefono);
  65. indice->siguiente=(struct agenda*)NULL;
  66. }
  67. /////////////////////////////////////////
  68. void lista_ficha(void){
  69. if(primero==(struct agenda*)NULL){
  70. for(i=2;i<=79;i++){
  71. gotoxy(i,13);printf(" ");}
  72. gotoxy((80-15)/2,13);printf("Lista vacia!!!!");
  73. getche();//retener mensaje
  74. return;}
  75. indice=primero;
  76. do{
  77. visu_ficha(indice);
  78. indice=indice->siguiente;
  79. }while(indice!=(struct agenda*)NULL);
  80. }
  81. ////////////////////////////////////////////////
  82. void muestra_ficha(void){
  83. char inbuf[30];
  84. for(i=2;i<=79;i++){
  85. gotoxy(i,13);printf(" ");}
  86. gotoxy(5,13);printf("Mostrar la ficha con Nombre: ");
  87. fflush(stdin);
  88. gets(inbuf);
  89. display_ficha(inbuf);}
  90. /////////////////////////////////////////////////////
  91. void visu_ficha(struct agenda *visu){
  92. for(i=2;i<=79;i++){
  93. gotoxy(i,13);printf(" ");
  94. gotoxy(i,16);printf(" ");
  95. gotoxy(i,17);printf(" ");
  96. gotoxy(i,18);printf(" ");}
  97. gotoxy(30,13);printf("Mostrando datos!!!!");
  98. gotoxy(5,16);printf("Nombre: %s",visu->nombre);
  99. gotoxy(5,17);printf("Direccion: %s",visu->direccion);
  100. gotoxy(5,18);printf("Telefono: %s",visu->telefono);
  101. if(borrando==0){
  102. hori(22);
  103. gotoxy(26,23);printf("Presiona CUALQUIER tecla para continuar");
  104. getche();}
  105. }
  106. ////////////////////////////////////////////////
  107. void borra_ficha(){
  108. struct agenda *borra;
  109. char inbuf[30];
  110. for(i=2;i<=79;i++){
  111. gotoxy(i,13);printf(" ");}
  112. gotoxy(5,13);printf("Borra la ficha con el nombre: ");
  113. fflush(stdin);
  114. gets(inbuf);
  115. borra= ((struct agenda*)display_ficha(inbuf));
  116. if(borra==(struct agenda*)NULL)
  117. return;
  118. for(i=2;i<=79;i++){
  119. gotoxy(i,13);printf(" ");}
  120. gotoxy(5,13);printf("Deseas borrar esta ficha? s/n: ");
  121. switch(toupper(getche())){
  122. case 'N':return;
  123. case 'S':break;}
  124. if(borra==primero)
  125. primero=primero->siguiente;
  126. else{
  127. indice=primero;
  128. while(indice->siguiente!=borra)
  129. indice=indice->siguiente;
  130. indice->siguiente=borra->siguiente;
  131. }
  132. free(borra);}
  133. //////////////////////////////////////////////////////
  134. struct agenda *display_ficha(char *n){
  135. struct agenda *ficha;
  136. for(i=2;i<=79;i++){
  137. gotoxy(i,13);printf(" ");}
  138. gotoxy((80-18)/2,13);printf("Buscando ficha!!!!");
  139. if(!(ficha=(struct agenda *)busca(n))){
  140. for(i=2;i<=79;i++){
  141. gotoxy(i,13);printf(" ");}
  142. gotoxy((80-23)/2,13);printf("Ficha no encontrada!!!!");
  143. getche();
  144. return NULL;
  145. }
  146. else
  147. visu_ficha(ficha);
  148. return ficha;}
  149. ///////////////////////////////////////////////////
  150. struct agenda *busca(char *inbuf){
  151. indice=primero;
  152. while(indice!=NULL){
  153. if(!strcmp(inbuf,indice->nombre))return indice;
  154. indice=indice->siguiente;}
  155. return NULL;}
  156. ///////////////////////////////////////////////
  157. void intro(){
  158. clrscr();
  159. margenes();
  160. gotoxy(3,2);printf("Programa No: 30");
  161. hori(3);
  162. gotoxy(3,4);printf("Lista enlazada");
  163. gotoxy(3,9);printf("Descripcion:");
  164. gotoxy(3,10);printf("Este programa maneja una lista enlazada");
  165. hori(22);
  166. gotoxy(20,23);printf("Presiona Cualquier tecla para continuar");
  167. getche();
  168. }
  169. /////////////////////////////////////////
  170. main(){
  171. clrscr();
  172. portada();
  173. intro();
  174. for(;;){
  175. clrscr();
  176. borrando=0;
  177. int opc;
  178. margenes();
  179. gotoxy((80-14)/2,2);cprintf("<--@G3|\|d@-->");
  180. hori(3);
  181. gotoxy((80-22)/2,4);cprintf("1.- Agregar");
  182. gotoxy((80-22)/2,5);cprintf("2.- Listar todos");
  183. gotoxy((80-22)/2,6);cprintf("3.- Mostrar por nombre");
  184. gotoxy((80-22)/2,7);cprintf("4.- Borrar por nombre");
  185. gotoxy((80-22)/2,8);cprintf("5.- Guargar a disco");
  186. gotoxy((80-22)/2,9);cprintf("6.- Leer de disco");
  187. gotoxy((80-22)/2,10);cprintf("7.- Salir");
  188. hori(12);
  189. hori(14);
  190. gotoxy((80-22)/2,13);cprintf("Elige tu opcion: ");
  191. scanf("%d",&opc);
  192. switch(opc){
  193. case 1:
  194. intro_ficha();
  195. break;
  196. case 2:
  197. lista_ficha();
  198. break;
  199. case 3:
  200. muestra_ficha();
  201. break;
  202. case 4:
  203. borrando=1;
  204. borra_ficha();
  205. break;
  206. case 5:
  207. almacena_datos();
  208. break;
  209. case 6:
  210. lee_datos();
  211. break;
  212. case 7:
  213. exit(0);
  214. break;}
  215. }
  216. }
  217. ////////////////////////////////////////////////
  218. void almacena_datos(void){
  219. FILE *ap;
  220. char *ruta=NULL;
  221. int c=0;
  222. for(i=2;i<=79;i++){
  223. gotoxy(i,13);printf(" ");}
  224. gotoxy((80-21)/2,13);printf("Guardando a disco!!!!");
  225. gotoxy(5,16);printf("Directorio: ");
  226. fflush(stdin);
  227. gets(ruta);
  228. if((ap=fopen(ruta,"wb"))==NULL){
  229. for(i=2;i<=79;i++){
  230. gotoxy(i,13);printf(" ");}
  231. gotoxy((80-31)/2,13);printf("El archivo no se puede abrir!!!!");
  232. getche();
  233. return;}
  234. indice=primero;
  235. for(i=2;i<=79;i++){
  236. gotoxy(i,13);printf(" ");}
  237. do{
  238. //efecto
  239. if(c==5){
  240. c=0;}
  241. if(c==0||c==3){
  242. gotoxy(39,13);printf("|");}
  243. if(c==1){
  244. gotoxy(39,13);printf("/");}
  245. if(c==2){
  246. gotoxy(39,13);printf("-");}
  247. if(c==4){
  248. gotoxy(39,13);printf("\\");}
  249. fwrite(indice,sizeof(struct agenda),1,ap);
  250. indice=indice->siguiente;
  251. c++;
  252. }while(indice!=(struct agenda*)NULL);
  253. fclose(ap);}
  254. ////////////////////////////////////////////
  255. void lee_datos(void){
  256. FILE *ap;
  257. char *ruta=NULL;
  258. for(i=2;i<=79;i++){
  259. gotoxy(i,13);printf(" ");}
  260. gotoxy((80-21)/2,13);printf("Leer datos de disco!!!!");
  261. gotoxy(5,16);printf("Directorio: ");
  262. fflush(stdin);
  263. gets(ruta);
  264. if((ap=fopen(ruta,"rb"))==NULL){
  265. for(i=2;i<=79;i++){
  266. gotoxy(i,13);printf(" ");}
  267. gotoxy((80-31)/2,13);printf("El archivo no se puede abrir!!!!");
  268. getche();
  269. return;}
  270. asigna_memoria();
  271. do{
  272. indice->siguiente=nuevo;
  273. indice=nuevo;
  274. fread(indice,sizeof(struct agenda),1,ap);
  275. nuevo=(struct agenda*)malloc(sizeof(struct agenda));
  276. }while(indice->siguiente!=NULL);
  277. fclose(ap);
  278. }
  279.  
  280.  
te pongo la portada por que si no deberas quitar algunas funciones
Código: Text
  1.  
  2. void portada();
  3. void margenes();
  4. void hori(int y);
  5. void oso(int x, int y);
  6. char c;
  7. int x,y;
  8. void portada(){
  9. clrscr();
  10. //hacer margenes//
  11. margenes();
  12. //datos inportantes
  13. textcolor(15);
  14. gotoxy(25,2);cprintf("INSTITUTO POLITECNICO NACIONAL");
  15. gotoxy(19,4);cprintf("INGENIERIA EN COMUNICACIONES Y ELECTRONICA");
  16. gotoxy(33,6);cprintf("COMPUTACION III");
  17. gotoxy(37,8);cprintf("Alumnos:");
  18. gotoxy(16,10);cprintf(" Parra Jimenez Pedro Alejandro Boleta: 2003300670");
  19. gotoxy(16,12);cprintf(" Hernandez Curiel Carlos       Boleta: 2003300000");
  20. gotoxy(36,14);cprintf("Grupo 3c5v");
  21. textcolor(GREEN+BLINK);gotoxy(12,16);cprintf("[*]_[.] '*-Presiona CUALQUIER tecla para continuar-*' [.]_[*]");
  22. getche();
  23. }//fin funcion principal
  24. void margenes(){
  25. textcolor(15);
  26. for(x=2;x<=79;x++){//margenes inferior/superior
  27. c=205;
  28. gotoxy(x,1);cprintf("%c",c);
  29. gotoxy(x,24);cprintf("%c",c);}
  30. //esquinas
  31. c=188;
  32. c=201;
  33. gotoxy(1,1);cprintf("%c",c);
  34. c=200;
  35. gotoxy(1,24);cprintf("%c",c);
  36. c=187;
  37. gotoxy(80,1);cprintf("%c",c);
  38. for(y=2;y<24;y++){//margenes laterasles
  39. c=186;
  40. gotoxy(1,y);cprintf("%c",c);
  41. gotoxy(80,y);cprintf("%c",c);}//fin margenes laterales
  42. c=188;
  43. gotoxy(80,24);cprintf("%c",c);
  44. }
  45. void hori(int y){
  46. //hori(No de linea)
  47. textcolor(15);
  48. gotoxy(80,y);cprintf("¹");
  49. gotoxy(1,y);cprintf("Ì");
  50. for (x=2;x<80;x++){
  51. gotoxy(x,y);cprintf("Í");}
  52. }
  53.  
  54. void oso(int x,int y){
  55. //declaracion
  56. //i coordenadas en x
  57. //j coordenada en y
  58. //j = Numero de linea; //colocar en la linea No
  59. //hori(i,j);}
  60. gotoxy(x,y);printf("   o,,,o");
  61. gotoxy(x,y+1);printf(" ( '&#59; ' )");
  62. gotoxy(x,y+2);printf("(,('''''),)");
  63. gotoxy(x,y+3);printf(" ('')'('')");
  64. }
  65.  
  66.  

Espero que esto te ayude

Suerte


...Perdon por el flower, pero fue sin querer, di responder sin querer, mil disculpas
UN intento de explicar lo inezplicable:D