0

I was writing a code that creates a list with a sublist from a txt file. But for some reason, after opening the file and trying to read the first line, the watches show that they stay empty. The file "equipos.txt" is in the same folder, and I've checked the structures and they all seemed fine. I have a similar code in another project that works fine, but it doesn't seem to work here.

void crealista(tlista* l){
    tlista nuevo,ant;
    tsublista subnuevo,subant;
    char vec[MAXnom];
    FILE *arch;
    arch=fopen("equipos.txt","rt");
    if(arch=fopen("equipos.txt","rt")== NULL){
        printf("Fallo al abrir archivo, puede no existir");
    }
    else{
        (*l)=NULL;
        ant=(*l);
        while(feof(arch) == 0){
            nuevo=(tlista)malloc(sizeof(nodo));
            fscanf(arch,"%s %d",nuevo->equipo,&nuevo->puntaje);
            nuevo->jugadores = NULL;
            nuevo->sig = NULL;
            fscanf(arch,"%s",vec);
            subant=(nuevo->jugadores);
            while(strcmp(vec,"ZZZ")!= 0){
                subnuevo=(tsublista)malloc(sizeof(nodito));
                strcpy(subnuevo->nombre,vec);
                fscanf(arch,"%d %c",&subnuevo->edad,&subnuevo->estado);
                subnuevo->sig = NULL;
                if(nuevo->jugadores == NULL){
                    nuevo->jugadores = subnuevo;
                }
                else{
                    subant->sig = subnuevo;
                    subant = subnuevo;
                }
                fscanf(arch,"%s",vec);
            }
            if((*l) == NULL){
                (*l) = nuevo;
            }
            else{
                ant->sig = nuevo;
                ant = nuevo;
            }
        }
    }
    fclose(arch);
}
  • 2
    `if(arch=fopen("equipos.txt","rt")== NULL)` -> `if((arch=fopen("equipos.txt","rt"))== NULL)` – kaylum May 13 '21 at 11:51
  • 2
    Please read [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – Some programmer dude May 13 '21 at 11:53
  • 2
    And please don't hide pointers behind type-aliases. That makes the code harder to read and understand. – Some programmer dude May 13 '21 at 11:54
  • 2
    And why do you think calling `fopen` twice in succession would be a good thing to do? – kaylum May 13 '21 at 11:54
  • 1
    Lastly there's some debate about [casting the result of `malloc`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858), and the majority consensus seems to be "don't do it". – Some programmer dude May 13 '21 at 11:55
  • 2
    And a function returning void and taking a pointer-to-pointer argument for the result is just silly. Why not return the actual result? – wildplasser May 13 '21 at 11:57
  • Error messages belong on stderr and should be informative: `const char *path = "equipos.txt"; if( (arch=fopen(path,"rt")) == NULL ){ perror(path); exit(EXIT_FAILURE); }` – William Pursell May 13 '21 at 12:28

1 Answers1

0
if(arch=fopen("equipos.txt","rt")== NULL) -> if((arch=fopen("equipos.txt","rt"))== NULL)

-kaylum

This seemed to do the trick.