#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 125
char archivo1[]="C:\\empleados.txt";
char archivo2[]="C:\\departamento.txt";
char archivo3[]="C:\\bueno.txt";
char *tmp;
typedef struct {
int ID;
char nombre[20];
int depto;
}empleado;
typedef struct {
int depto;
char nom_dep[20];
}departamento;
typedef struct {
int ID;
char nombre[20];
char depa[20];
}combina;
empleado separar(char *temporal)
{
empleado emp;
tmp=strtok(temporal,",");
emp.ID=atoi(tmp);
tmp=strtok(NULL,",");
strcpy(emp.nombre,tmp);
tmp=strtok(NULL,"\n");
emp.depto=atoi(tmp);
printf("\n%d %s %d",emp.ID,emp.nombre,emp.depto);
return emp;
}
departamento separar2(char *temporal)
{
departamento dep;
tmp=strtok(temporal,",");
dep.depto=atoi(tmp);
tmp=strtok(NULL,"\n");
strcpy(dep.nom_dep,tmp);
printf("\n%d %s",dep.depto,dep.nom_dep);
return dep;
}
int main()
{
FILE *arch1, *arch2, *arch3;
char temporal[MAX];
int i=0,j,k,tame=0,tamd=0,band;
empleado tempor[10];
departamento tempo[10];
combina aux[10];
arch1=fopen(archivo1,"r+");
arch2=fopen(archivo2,"r+");
if(arch1==NULL || arch2==NULL)
printf("Error de apertura");
else
{
fgets(temporal,MAX,arch1);
printf("leyendo empleados\n");
while(!feof(arch1))
{
tempor[i]=separar(temporal);
fgets(temporal,MAX,arch1);
i++;
}
//fgets
tame=i++;
i=0;
fgets(temporal,MAX,arch2);
printf("leyendo depto\n");
while(!feof(arch2))
{
tempo[i]=separar2(temporal);
fgets(temporal,MAX,arch2);
i++;
}
tamd=i++;
}
printf("\n\n");
for(i=0;i<tame;i++)
printf("%d %s %d \n",tempor[i].ID,tempor[i].nombre,tempor[i].depto);
printf("\n\n");
for(i=0;i<tamd;i++)
printf("%d %s \n",tempo[i].depto,tempo[i].nom_dep);
printf("\n\n");
for(j=0;j<tame;j++)
{
aux[j].ID = tempor[j].ID;
memset(aux[j].nombre,' ',21);
memset(aux[j].depa,' ',21);
strcpy(aux[j].nombre ,tempor[j].nombre);
band=0;
for(k=0;k<=tamd;k++)
{
if(tempor[j].depto==tempo[k].depto)
{
strcpy(aux[j].depa,tempo[k].nom_dep );
band=1;
}
}
if(band==0)
strcpy(aux[j].depa,"Desconocido");
}
for(i=0;i<tame;i++)
{
printf("%s %s %d \n",aux[i].nombre,aux[i].depa,aux[i].ID);
}
arch3=fopen(archivo3,"w+");
if(arch3!=NULL)
{
for(j=0;j<tame;j++)
{
fprintf(arch3,"%s %s %d \n",aux[j].nombre,aux[j].depa,aux[j].ID);
}
}
else printf("No se pudo abrir el 3 archivo\n");
fclose(arch1);
fclose(arch2);
fclose(arch3);
system("pause");
return EXIT_SUCCESS;
}