• Domingo 19 de Mayo de 2024, 12:25

Autor Tema:  Problema Guardar Registros  (Leído 1196 veces)

Iganguli

  • Miembro activo
  • **
  • Mensajes: 51
  • Nacionalidad: mx
    • Ver Perfil
Problema Guardar Registros
« en: Viernes 14 de Marzo de 2008, 13:53 »
0
hola tengo dudas en este programa lo que tiene que hacer es leer datos de 2 archivos y combinarlos en uno mismo asi por ejemplo el archivo empleados  tiene
1,jose,10
2,luisa,20
3,juan,5
y el archivo departamento tiene
10,sistemas
5,administracion
20,soporte
entonces en el archivo bueno.txt deberia guardar
1,jose,sistemas
2,luisa,soporte
3,juan,administracion
el prolema es que si lee bien los registros del archivo empleado y departamento pero al momento de guardarlos al 3 archivo ya no lo guarda si alguien e echa una mano gracias


Código: Text
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #define MAX 125
  7.  
  8. char archivo1[]="C:\\empleados.txt";
  9. char archivo2[]="C:\\departamento.txt";
  10. char archivo3[]="C:\\bueno.txt";
  11.  
  12. typedef struct {
  13.   int ID;
  14.   char nombre[30];
  15.   int depto;
  16.   }empleado;
  17.  
  18. typedef struct {
  19.   int depto;
  20.   char nom_dep[30];
  21.   }departamento;
  22.  
  23. typedef struct {
  24.   int ID;
  25.   char nombre[30];
  26.   char depa[30];
  27.   }combina;
  28.  
  29. empleado separar(char *temporal)
  30. {
  31.  empleado emp;
  32.  memset(emp.nombre,' ',30);
  33.  char aux[]=",";
  34.  char *tmp;
  35.  tmp=strtok(temporal,aux);
  36.  emp.ID=atoi(tmp);
  37.  tmp=strtok(NULL,aux);
  38.  strcpy(emp.nombre,tmp);
  39.  tmp=strtok(NULL,aux);
  40.  emp.depto=atoi(tmp);
  41.  printf("\n%d %s %d",emp.ID,emp.nombre,emp.depto);
  42.  return emp;
  43. }
  44.  
  45. departamento separar2(char *temporal)
  46. {
  47.  departamento dep;
  48.  memset(dep.nom_dep,' ',30);
  49.  char aux[]=",";
  50.  char *tmp;
  51.  tmp=strtok(temporal,aux);
  52.  dep.depto=atoi(tmp);
  53.  tmp=strtok(NULL,aux);
  54.  strcpy(dep.nom_dep,tmp);
  55.  printf("\n%d %s",dep.depto,dep.nom_dep);
  56.  return dep;
  57.  }
  58.  
  59.  
  60.  
  61. int main()
  62. {
  63.     FILE *arch1, *arch2, *arch3;
  64.     char temporal[MAX];
  65.     int i=0,k,tame=0,tamd=0;
  66.     empleado tempor[10];
  67.     departamento tempo[10];
  68.     combina aux[10];
  69.     arch1=fopen(archivo1,"r+");
  70.     arch2=fopen(archivo2,"r+");
  71.     if(arch1==NULL || arch2==NULL)
  72.      printf("Error de apertura");
  73.     else
  74.     {
  75.   fgets(temporal,MAX,arch1);
  76.   while(!feof(arch1))
  77.   {
  78.   tempor[i]=separar(temporal);
  79.   fgets(temporal,MAX,arch1);
  80.   i++;
  81.   }
  82.   tempor[i]=separar(temporal);
  83.   tame=i++;
  84.   i=0;
  85.   fgets(temporal,MAX,arch2);
  86.   while(!feof(arch2))
  87.   {
  88.   tempo[i]=separar2(temporal);
  89.   fgets(temporal,MAX,arch2);
  90.   i++;
  91.   }
  92.   tempo[i]=separar2(temporal);
  93.   tamd=i++;
  94.     }
  95.     for(int j=0;j<tame;j++)
  96.     {
  97.    
  98.     aux[i].ID = tempor[i].ID&#59;
  99.     strcpy(aux[i].nombre ,tempor[i].nombre );
  100.     for(int k=0;k<10;k++)
  101.       if(tempor[i].depto==tempo[k].depto)
  102.         strcpy(aux[i].depa,tempo[k].nom_dep );
  103.   }
  104.     arch3=fopen(archivo3,"w+");
  105.     if(arch3!=NULL)
  106.     {
  107.     for(int j=0;j<tame;j++)
  108.       {
  109.       fwrite(&aux[j],90,1,arch3);
  110.       }
  111.     }
  112.     else printf("No se pudo abrir el 3 archivo\n");
  113.     fclose(arch1);
  114.     fclose(arch2);
  115.     fclose(arch3);
  116.     system("pause");
  117.     return EXIT_SUCCESS;
  118. }
  119.  
  120.  
  121.  

Iganguli

  • Miembro activo
  • **
  • Mensajes: 51
  • Nacionalidad: mx
    • Ver Perfil
Re: Problema Guardar Registros
« Respuesta #1 en: Viernes 14 de Marzo de 2008, 14:04 »
0
al paracer tenia mal mas que eso pero ya lo resolvi pero ahora como lo guardo ya que nada mas guarda el nombre y el area pero el id no de nuevo alguien que me eche la mano

Código: Text
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #define MAX 125
  7.  
  8. char archivo1[]="C:\\empleados.txt";
  9. char archivo2[]="C:\\departamento.txt";
  10. char archivo3[]="C:\\bueno.txt";
  11. char *tmp;
  12.  
  13. typedef struct {
  14.   int ID;
  15.   char nombre[20];
  16.   int depto;
  17.   }empleado;
  18.  
  19. typedef struct {
  20.   int depto;
  21.   char nom_dep[20];
  22.   }departamento;
  23.  
  24. typedef struct {
  25.   int ID;
  26.   char nombre[20];
  27.   char depa[20];
  28.   }combina;
  29.  
  30. empleado separar(char *temporal)
  31. {
  32.  empleado emp;
  33.  memset(emp.nombre,' ',20);
  34.  tmp=strtok(temporal,",");
  35.  emp.ID=atoi(tmp);
  36.  tmp=strtok(NULL,",");
  37.  strcpy(emp.nombre,tmp);
  38.  tmp=strtok(NULL,",");
  39.  emp.depto=atoi(tmp);
  40.  printf("\n%d %s %d",emp.ID,emp.nombre,emp.depto);
  41.  return emp;
  42. }
  43.  
  44. departamento separar2(char *temporal)
  45. {
  46.  departamento dep;
  47.  memset(dep.nom_dep,' ',20);
  48.  tmp=strtok(temporal,",");
  49.  dep.depto=atoi(tmp);
  50.  tmp=strtok(NULL,"\n");
  51.  strcpy(dep.nom_dep,tmp);
  52.  printf("\n%d %s",dep.depto,dep.nom_dep);
  53.  return dep;
  54. }
  55.  
  56.  
  57.  
  58. int main()
  59. {
  60.     FILE *arch1, *arch2, *arch3;
  61.     char temporal[MAX];
  62.     int i=0,j,k,tame=0,tamd=0;
  63.     empleado tempor[10];
  64.     departamento tempo[10];
  65.     combina aux[10];
  66.     arch1=fopen(archivo1,"r+");
  67.     arch2=fopen(archivo2,"r+");
  68.     if(arch1==NULL || arch2==NULL)
  69.      printf("Error de apertura");
  70.     else
  71.     {
  72.   fgets(temporal,MAX,arch1);
  73.   while(!feof(arch1))
  74.   {
  75.   tempor[i]=separar(temporal);
  76.   fgets(temporal,MAX,arch1);
  77.   i++;
  78.   }
  79.   tempor[i]=separar(temporal);
  80.   tame=i++;
  81.   i=0;
  82.   fgets(temporal,MAX,arch2);
  83.   while(!feof(arch2))
  84.   {
  85.   tempo[i]=separar2(temporal);
  86.   fgets(temporal,MAX,arch2);
  87.   i++;
  88.   }
  89.   tempo[i]=separar2(temporal);
  90.   tamd=i++;
  91.     }
  92.     for(i=0;i<=tame;i++)
  93.     printf("%d %s %d \n",tempor[i].ID,tempor[i].nombre,tempor[i].depto);
  94.     for(i=0;i<=tamd;i++)
  95.     printf("%d %s \n",tempo[i].depto,tempo[i].nom_dep);
  96.     for(j=0;j<=tame;j++)
  97.     {
  98.     aux[j].ID = tempor[j].ID;
  99.     memset(aux[j].nombre,' ',21);
  100.     memset(aux[j].depa,' ',21);
  101.     strcpy(aux[j].nombre ,tempor[j].nombre );
  102.     for(k=0;k<=tamd;k++)
  103.      if(tempor[j].depto==tempo[k].depto)
  104.       strcpy(aux[j].depa,tempo[k].nom_dep );
  105.     }
  106.     for(i=0;i<tame;i++)
  107.     {
  108.      printf("%s %s %d \n",aux[i].nombre,aux[i].depa,aux[i].ID);
  109.  
  110.     }
  111.     arch3=fopen(archivo3,"w+");
  112.     if(arch3!=NULL)
  113.     {
  114.     for(j=0;j<tame;j++)
  115.       {
  116.       fwrite(&aux[j],sizeof(combina),1,arch3);
  117.       }
  118.     }
  119.     else printf("No se pudo abrir el 3 archivo\n");
  120.     fclose(arch1);
  121.     fclose(arch2);
  122.     fclose(arch3);
  123.     system("pause");
  124.     return EXIT_SUCCESS;
  125. }
  126.  
  127.  

Iganguli

  • Miembro activo
  • **
  • Mensajes: 51
  • Nacionalidad: mx
    • Ver Perfil
Re: Problema Guardar Registros
« Respuesta #2 en: Domingo 16 de Marzo de 2008, 07:29 »
0
bueno ahora si creo que ya encontre el problema y le cambie el fwrite al fpintf les anexo el codigo por si ha alguien le interesa

Código: Text
  1.  
  2. #include <stdio.h>
  3. #include <conio.h>
  4. #include <string.h>
  5. #include <stdlib.h>
  6. #define MAX 125
  7.  
  8. char archivo1[]="C:\\empleados.txt";
  9. char archivo2[]="C:\\departamento.txt";
  10. char archivo3[]="C:\\bueno.txt";
  11. char *tmp;
  12.  
  13. typedef struct {
  14.  int ID;
  15.  char nombre[20];
  16.  int depto;
  17.  }empleado;
  18.  
  19. typedef struct {
  20.  int depto;
  21.  char nom_dep[20];
  22.  }departamento;
  23.  
  24. typedef struct {
  25.  int ID;
  26.  char nombre[20];
  27.  char depa[20];
  28.  }combina;
  29.  
  30. empleado separar(char *temporal)
  31. {
  32. empleado emp;
  33. tmp=strtok(temporal,",");
  34. emp.ID=atoi(tmp);
  35. tmp=strtok(NULL,",");
  36. strcpy(emp.nombre,tmp);
  37. tmp=strtok(NULL,"\n");
  38. emp.depto=atoi(tmp);
  39. printf("\n%d %s %d",emp.ID,emp.nombre,emp.depto);
  40. return emp;
  41. }
  42.  
  43. departamento separar2(char *temporal)
  44. {
  45. departamento dep;
  46. tmp=strtok(temporal,",");
  47. dep.depto=atoi(tmp);
  48. tmp=strtok(NULL,"\n");
  49. strcpy(dep.nom_dep,tmp);
  50. printf("\n%d %s",dep.depto,dep.nom_dep);
  51. return dep;
  52. }
  53.  
  54. int main()
  55. {
  56.    FILE *arch1, *arch2, *arch3;
  57.    char temporal[MAX];
  58.    int i=0,j,k,tame=0,tamd=0,band;
  59.    empleado tempor[10];
  60.    departamento tempo[10];
  61.    combina aux[10];
  62.    arch1=fopen(archivo1,"r+");
  63.    arch2=fopen(archivo2,"r+");
  64.    if(arch1==NULL || arch2==NULL)
  65.     printf("Error de apertura");
  66.    else
  67.    {
  68.    fgets(temporal,MAX,arch1);
  69.    printf("leyendo empleados\n");
  70.    while(!feof(arch1))
  71.    {
  72.    tempor[i]=separar(temporal);
  73.    fgets(temporal,MAX,arch1);
  74.    i++;
  75.    }
  76.    //fgets
  77.    tame=i++;
  78.    i=0;
  79.    fgets(temporal,MAX,arch2);
  80.       printf("leyendo depto\n");
  81.    while(!feof(arch2))
  82.    {
  83.    tempo[i]=separar2(temporal);
  84.    fgets(temporal,MAX,arch2);
  85.    i++;
  86.    }
  87.    tamd=i++;
  88.    }
  89.    printf("\n\n");
  90.    for(i=0;i<tame;i++)
  91.    printf("%d %s %d \n",tempor[i].ID,tempor[i].nombre,tempor[i].depto);
  92.    printf("\n\n");
  93.    for(i=0;i<tamd;i++)
  94.    printf("%d %s \n",tempo[i].depto,tempo[i].nom_dep);
  95.    printf("\n\n");
  96.    for(j=0;j<tame;j++)
  97.    {
  98.    aux[j].ID = tempor[j].ID;
  99.    memset(aux[j].nombre,' ',21);
  100.    memset(aux[j].depa,' ',21);
  101.    strcpy(aux[j].nombre ,tempor[j].nombre);
  102.    band=0;
  103.    for(k=0;k<=tamd;k++)
  104.    {
  105.     if(tempor[j].depto==tempo[k].depto)
  106.     {
  107.     strcpy(aux[j].depa,tempo[k].nom_dep );
  108.     band=1;
  109.     }
  110.    }
  111.    if(band==0)
  112.   strcpy(aux[j].depa,"Desconocido");
  113.    }
  114.    for(i=0;i<tame;i++)
  115.    {
  116.     printf("%s %s %d \n",aux[i].nombre,aux[i].depa,aux[i].ID);
  117.    }
  118.    arch3=fopen(archivo3,"w+");
  119.    if(arch3!=NULL)
  120.    {
  121.    for(j=0;j<tame;j++)
  122.      {
  123.      fprintf(arch3,"%s %s %d \n",aux[j].nombre,aux[j].depa,aux[j].ID);
  124.      }
  125.    }
  126.    else printf("No se pudo abrir el 3 archivo\n");
  127.    fclose(arch1);
  128.    fclose(arch2);
  129.    fclose(arch3);
  130.    system("pause");
  131.    return EXIT_SUCCESS;
  132. }
  133.  
  134.