Error when trying to get data from a .txt file and move to a queue in C?

1

I can open and move through the file the error happens when I try to point to the beginning or end of the queue, someone tell me if the pointers are correct

typedef struct lista {
int info;
char infoP;
struct lista* prox;
}Lista;

typedef struct fila {
Lista* ini;
Lista* fim;
}Fila;

void fila_insere (Fila* f)
{
Lista* n = (Lista*) malloc(sizeof(Lista));
n->prox = NULL;
FILE *Arquivo;
Arquivo = fopen("..\senha.txt", "r");

do
{
n->info = getc(Arquivo);
if(Arquivo == 0)
{
    printf("Erro na abertura do arquivo");
    fclose(Arquivo);
}
else
{
    if( n->info != '\n' && n->info != EOF)
    {
        if(lst_vazia(f))
        {
           f->fim->prox = n;
        }
        else
        {
            f->ini = n;
        }
        f->fim = n;
    }
 }
}
while (n->info != EOF);
}

Sample file that the program will read

 1
 2
 3
 4
    
asked by anonymous 11.04.2017 / 20:51

1 answer

1

Your code is incomplete, so it's difficult to answer directly, what you can do is give you a generic answer.

The code below creates a queue and inserts values, which can be used as a basis for solving your problem. Just replace the for of main with something that takes the values from the text document and insert it into the queue using the Fila* insereFila(Fila* f, int info) function.

#include<stdio.h>

typedef struct lista{
    int info;
    struct lista* prox;
} Lista;

typedef struct fila{
    Lista* ini;
    Lista* fim;
} Fila;

Fila* criaFila(Fila* f, int info)
{
    Lista* novo = (Lista*) malloc(sizeof(Lista));
    novo->prox = NULL;
    novo->info = info;
    f = (Fila*) malloc(sizeof(Fila));
    f->ini = novo;
    f->fim = novo;
    return f;
}

Fila* insereFila(Fila* f, int info)
{

    if(f==NULL)
        return criaFila(f, info);

    Lista* novo = (Lista*) malloc(sizeof(Lista));
    novo->prox = f->ini;
    novo->info = info;

    f->ini = novo;

    return f;
}


void imprimeFila(Fila* f)
{
    printf("---- fila -----\n");
    Lista* l = f->ini;
    do
    {
        printf("%d\n", l->info);
        l= l->prox;
    }
    while(l != NULL);
}

int main()
{
    Fila* f = NULL;

    int i;
    for(i=0; i<5; i++)
        f = insereFila(f, i);

    imprimeFila(f);
    return 0;
}
    
18.04.2017 / 11:02