Foro de elhacker.net

Programación => Programación C/C++ => Mensaje iniciado por: clodan en 14 Octubre 2010, 15:47 pm



Título: Manejo de Archivos - PROBLEMA!!!
Publicado por: clodan en 14 Octubre 2010, 15:47 pm
Bueno, este es el codigo que tengo:

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. struct sis{
  5.       int cod;
  6.       char titu[25];
  7.       char aut[25];
  8.       char edit[25];
  9.       int ano;
  10.       char gene[25];
  11.       };
  12.  
  13. int agregar();
  14. int ver();
  15.  
  16. char * prog="DATA.file";
  17. FILE * pf = NULL;
  18.  
  19. struct sis base[3000]={0};
  20.  
  21. int main(int argc, char *argv[]){
  22.  
  23.  int x;
  24.  pf = fopen( prog, "a+b");
  25.  if (pf==NULL){
  26.        printf("Error al Crear, Abrir o Modificar el Archivo");
  27.        while (getchar()!='\n');
  28.        return 0;
  29.        }
  30.  printf("DATA.file abierto.\n");
  31.  printf("Leyendo Datos de DATA.file. Por favor espere...\n\n");
  32.  while (!feof(pf)){
  33.        fread(&base, sizeof(base), 1, pf);
  34.        }
  35.  
  36.  
  37.  
  38.  printf("....Sistema Interno....\n\n");
  39.  printf("Eliga la opcion que desea realizar:\n");
  40.  printf("1.Agregar Libro Nuevo\n");
  41.  printf("2.Ver Libros Actuales\n");
  42.  scanf("%d",&x);
  43.  switch (x){
  44.         case 1:{
  45.            agregar();
  46.            break;
  47.         }
  48.         case 2:{
  49.            ver();
  50.            break;
  51.         }
  52.  }
  53.  
  54.  fclose(pf);
  55.  system("PAUSE");
  56.  return 0;
  57. }
  58.  
  59. int agregar(){
  60.    int z=0,y=0;
  61.    if (base[z].cod==0){
  62.       y=z;
  63.       }
  64.    else {
  65.       z++;
  66.       }
  67.  
  68.    base[y].cod=y+1;  
  69.    printf("Ingrese el Titulo del Libro\n");
  70.    scanf("%s",&base[y].titu);
  71.    printf("Ingrese el Autor del Libro\n");
  72.    scanf("%s",&base[y].aut);
  73.    printf("Ingrese la Editorial del Libro\n");
  74.    scanf("%s",&base[y].edit);
  75.    printf("Ingrese el ano del Libro\n");
  76.    scanf("%d",&base[y].ano);
  77.    printf("Ingrese el genero del Libro\n");
  78.    scanf("%d",&base[y].gene);
  79.  
  80.    fwrite (base, 1 , sizeof(base) , pf );
  81.  
  82.    system("PAUSE");
  83.    return 0;
  84.    }
  85.  
  86. int ver(){
  87.    int i=0;
  88.    int z=0,y=0;
  89.    if (base[z].cod==0){
  90.       y=z;
  91.       }
  92.    else {
  93.       z++;
  94.       }
  95.  
  96.    for (i=0; i<=y; i++){
  97.    printf("%d \t %s \t %s \t %s \t %d \t %s\n",base[i].cod,base[i].titu,base[i].aut,base[i].edit,base[i].ano,base[i].gene );
  98.    }
  99.  
  100.    return 0;
  101.    }
  102.  


El gran problema, es qe me sobreescribe la informacion que ya esta en el archivo, osea... lo que necesito es...

que lea los datos del archivo y los meta en el struct BASE. Despues, cuando se desea hacer algo, que el programa vea cual es el ultimo registro, y agregue algo despues de ese. Finalmente que lo guarde en el archivo sobreescribiendo lo anterior.

Lo que sucede es que abre el archivo, lo lee, pero cuando escribe, me escribe arriba de lo que ya estaba antes :s


Título: Re: Manejo de Archivos - PROBLEMA!!!
Publicado por: Horricreu en 14 Octubre 2010, 16:35 pm
Puedes usar la función fseek() (http://www.cplusplus.com/reference/clibrary/cstdio/fseek/) de la librería stdio.h.


Título: Re: Manejo de Archivos - PROBLEMA!!!
Publicado por: clodan en 14 Octubre 2010, 17:52 pm
MODIFICACION

Ya solucione el problema que tenia, ahora el problema que tengo es que me muestra 2 veces la ultima informacion guardada  >:(

Código
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct{
  5.       int cod;
  6.       char titu[25];
  7.       char aut[25];
  8.       char edit[25];
  9.       int ano;
  10.       char gene[25];
  11.       }sis;
  12. int agregar();
  13. int ver();
  14. char * prog="DATA.file";
  15. FILE * pf = NULL;
  16. sis base;
  17. int main(int argc, char *argv[]){
  18.  int x;
  19.  pf = fopen( prog, "a+b");
  20.  if (pf==NULL){
  21.        printf("Error al Crear, Abrir o Modificar el Archivo");
  22.        while (getchar()!='\n');
  23.        return 0;
  24.        }
  25.  printf("DATA.file abierto.\n");
  26.  printf("Leyendo Datos de DATA.file. Por favor espere...\n\n");
  27.  while (!feof(pf)){
  28.        fread(&base, sizeof(sis), 1, pf);
  29.  }
  30.  printf("....Sistema Interno....\n\n");
  31.  printf("Eliga la opcion que desea realizar:\n");
  32.  printf("1.Agregar Libro Nuevo\n");
  33.  printf("2.Ver Libros Actuales\n");
  34.  scanf("%d",&x);
  35.  switch (x){
  36.         case 1:{
  37.            agregar();
  38.            break;
  39.         }
  40.         case 2:{
  41.            ver();
  42.            break;
  43.         }
  44.  }
  45.  fclose(pf);
  46.  fflush(stdin);
  47.  while (getchar()!='\n');
  48.  return 0;
  49. }
  50.  
  51. int agregar(){
  52.  
  53.    printf("Ingrese el Titulo del Libro\n");
  54.    fflush(stdin);
  55.    gets(base.titu);
  56.    printf("Ingrese el Autor del Libro\n");
  57.    fflush(stdin);
  58.    gets(base.aut);
  59.    printf("Ingrese la Editorial del Libro\n");
  60.    fflush(stdin);
  61.    gets(base.edit);
  62.    printf("Ingrese el ano del Libro\n");
  63.    fflush(stdin);
  64.    scanf("%d",&base.ano);
  65.    printf("Ingrese el genero del Libro\n");
  66.    fflush(stdin);
  67.    gets(base.gene);
  68.  
  69.    fwrite (&base, 1, sizeof(sis), pf);
  70. return 0;
  71. }
  72.  
  73. int ver(){
  74.    rewind(pf);
  75.    while (!feof(pf)){
  76.          fread(&base, sizeof(sis), 1, pf);
  77.          printf("%d - %s - %s - %s - %d - %s\n",base.cod,base.titu,base.aut,base.edit,base.ano,base.gene );
  78.    }
  79.  
  80. return 0;
  81. }


Título: Re: Manejo de Archivos - PROBLEMA!!!
Publicado por: Horricreu en 14 Octubre 2010, 18:34 pm
Cambia el while() por un if() y verás como funciona.