Reputation: 21
I'm trying to read a .txt file like this:
- 20590772-6
- Raul
- ceo
- 300001
- 20535787-4
- Valentin
- ceo
- 300002
- 12345678-9
- empleado1
- empleado
- 400000
- 23456789-0
- empleado2
- empleado
- 490900
- 34567890-8
- empleado3
- empleado
- 999999
Every 4 lines are part of a structure and for a reason when I'm printing it the first and second line combines showing for example "12345567-8Name" and not "12345567-8"
Here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct empleado{
char rut_emp[10];
char nombre_emp[20];
char cargo_emp[20];
int sbase;
struct empleado *siguiente;
}empleado;
struct empleado *primero, *ultimo;
void leerArchivo(empleado lista[]){
FILE *fp;
fp = fopen("empleados.txt", "r");
int cont=0;
int cont_emp=0;
static char linea[5000];
for(int i = 0; i < 20; i++){
if(fgets(linea, 5000, fp) != NULL) {
linea[strcspn(linea, "\r\n")] = 0;
char delim[] = " ";
char *palabra_num = strtok(linea, delim);
if(cont>=0 && cont<4){
if(cont==3){
lista[cont_emp].sbase=atoi(linea);
cont_emp++;
cont=0;
}else if(cont==2){
strcpy(lista[cont_emp].cargo_emp,linea);
cont++;
}else if(cont==1){
strcpy(lista[cont_emp].nombre_emp,linea);
cont++;
}else if(cont==0){
strcpy(lista[cont_emp].rut_emp,linea);
cont++;
}
}
}
}
fclose(fp);
}
int main(void) {
empleado lista_emp[5];
leerArchivo(lista_emp);
printf("Empleados: \n");
for(int i=0;i<5;i++){
printf("Rut: %s\n",lista_emp[i].rut_emp);
printf("Nombre: %s\n",lista_emp[i].nombre_emp);
printf("Cargo: %s\n",lista_emp[i].cargo_emp);
printf("Numero: %i\n",lista_emp[i].sbase);
}
return 0;
}
Upvotes: 0
Views: 71
Reputation: 753475
Here's a minor variant of your code, with the size of rut_emp
increased from 10 to 11, and with some error checking.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct empleado
{
char rut_emp[11];
char nombre_emp[20];
char cargo_emp[20];
int sbase;
struct empleado *siguiente;
} empleado;
struct empleado *primero, *ultimo;
static void leerArchivo(empleado lista[])
{
const char filename[] = "empleados.txt";
FILE *fp = fopen(filename, "r");
if (fp == NULL)
{
fprintf(stderr, "failed to open file %s for reading\n", filename);
exit(EXIT_FAILURE);
}
int cont = 0;
int cont_emp = 0;
static char linea[5000];
for (int i = 0; i < 20; i++)
{
if (fgets(linea, 5000, fp) != NULL)
{
linea[strcspn(linea, "\r\n")] = 0;
if (cont >= 0 && cont < 4)
{
if (cont == 3)
{
lista[cont_emp].sbase = atoi(linea);
cont_emp++;
cont = 0;
}
else if (cont == 2)
{
if (strlen(linea) >= sizeof(lista[cont_emp].cargo_emp))
{
fprintf(stderr, "Cargo Emp '%s' is too long\n", linea);
exit(EXIT_FAILURE);
}
strcpy(lista[cont_emp].cargo_emp, linea);
cont++;
}
else if (cont == 1)
{
if (strlen(linea) >= sizeof(lista[cont_emp].nombre_emp))
{
fprintf(stderr, "Nombre Emp '%s' is too long\n", linea);
exit(EXIT_FAILURE);
}
strcpy(lista[cont_emp].nombre_emp, linea);
cont++;
}
else if (cont == 0)
{
if (strlen(linea) >= sizeof(lista[cont_emp].rut_emp))
{
fprintf(stderr, "RUT Emp '%s' is too long\n", linea);
exit(EXIT_FAILURE);
}
strcpy(lista[cont_emp].rut_emp, linea);
cont++;
}
}
}
}
fclose(fp);
}
int main(void)
{
empleado lista_emp[5];
leerArchivo(lista_emp);
printf("Empleados:\n");
for (int i = 0; i < 5; i++)
{
printf("Rut %d: %s\n", i, lista_emp[i].rut_emp);
printf("Nombre: %s\n", lista_emp[i].nombre_emp);
printf("Cargo: %s\n", lista_emp[i].cargo_emp);
printf("Numero: %i\n", lista_emp[i].sbase);
}
return 0;
}
With your data file, this produces the output:
Empleados:
Rut 0: 20590772-6
Nombre: Raul
Cargo: ceo
Numero: 300001
Rut 1: 20535787-4
Nombre: Valentin
Cargo: ceo
Numero: 300002
Rut 2: 12345678-9
Nombre: empleado1
Cargo: empleado
Numero: 400000
Rut 3: 23456789-0
Nombre: empleado2
Cargo: empleado
Numero: 490900
Rut 4: 34567890-8
Nombre: empleado3
Cargo: empleado
Numero: 999999
If you were still running into problems, I have to deduce that you didn't make the change correctly, or you didn't recompile.
Upvotes: 1