Reading files in c

0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct pila{
   int clave;
   char nombre[50];
   float salario;
   struct pila *psig;
}pila;

pila *pmaestro;
pila *NuevoElem();
void push(int clave,char nombre[],float salario);
void imprimir(pila *);

int main()
{
    char nmbre[50];
    int clve;
    float slrio;
    FILE *f1;
    f1=fopen("listas.txt","r");
    if(f1!=NULL){
            // ********
            while(!feof(f1)){
                fscanf(f1,"%d,%s,%f",&clve,nmbre,&slrio);
                push(clve,nmbre,slrio);
            }
            // ********
    }else{
        printf("Error de apertura");
    }
    imprimir(pmaestro);
    fclose(f1);
 return 0;
}

void push(int clave,char nombre[],float salario)
{ pila *q = NuevoElem();
   q->psig = pmaestro;
   q->clave = clave;
   strcpy(q->nombre,nombre);
   q->salario = salario;
   pmaestro = q;
}

pila *NuevoElem()
{ pila *q = (pila *)malloc(sizeof(pila));
  if (q==NULL) { printf("Falta memoria"); exit(0);}
  return q;
}

void imprimir(pila *pmaestro)
{
   while(pmaestro!=NULL)
   {
      printf("%d\t%s\t%f", pmaestro->clave,pmaestro->nombre,pmaestro->salario);
      pmaestro = pmaestro->psig;
   }
   printf("\n");
}

This code must read a file that contains a series of keys, names and salaries. At the moment of wanting to read the file, for some reason the while is cycled and it does not advance from there. Why do you cycle?

    
asked by Choy Gonzalez 03.08.2018 в 02:29
source

1 answer

0

From what I saw, you have 2 problems in your code. The first one is in fscanf() in the format string. You indicated "%d,%s,%f" , could deduce that your file lista.txt has a format of type <Clave>,<Nombre>,<Salario> , in case of error, please tell me how is the correct format. The error here is that when you read the format %s you will read name and salary together. Since the reading is done until you find a space or a '\n' .

If you can modify the text file, simply add, instead of a comma, a space between the name and salary field. Otherwise, I recommend you change your reading strategy.

Some ideas I can think of is to use fgets to get the whole line of the file (remember that this function captures the '\n' , so you will have to remove it in some way and separate the fields.

The second error is in this code fragment

 while(!feof(f1)){
    fscanf(f1,"%d,%s %f",&clve,nmbre,&slrio);
    push(clve,nmbre,slrio);
 }

If you place this way the fscanf or any method to read the contents of the file you will enter 1 time more than the due time. I give you an example with the following data:

 1,Juan 107.50
 2,Ana 109.25

PS: in this case I assumed that the modification explained above could be made to change the , for a space.

In the first stage of the loop, your active record indicator points to the record of Juan , you read the content and you store it in the stack. As it is not the end of the file, I continue to enter the while (the record indicator is still pointed to Juan )

In the second stage the record of Ana is read (the last record of the file) and inserted. Reassess the condition of the while, return true since the pointer is in the Ana register but this is already an error since there are no more records to read or store and what you are inserting into the stack is garbage .

This would solve it in the following way:

fscanf(f1, "%d,%s %f", &clve, nmbre, &slrio);
        while (!feof(f1)) {         
            push(clve, nmbre, slrio);
            fscanf(f1, "%d,%s %f", &clve, nmbre, &slrio);
        }

Thing of already reading the first record and storing it in the list (as long as there is more than 1 record). And in this way you avoid reading garbage. Or you could add an if asking if it is the end of the file and leave the loop or as you like.

I also found in the show function, that it is not an error, that you forgot the \n after showing the contents of the stack. To visualize each record one below the other and not all followed.

I hope it has helped you and if something was not understood or I did not explain, let me know.

Greetings

    
answered by 03.08.2018 / 17:58
source