Error trying to grab data from a . txt file and move to a C queue?

Asked

Viewed 100 times

1

I can open and go through the entire 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);
}

Example of file the program will read

 1
 2
 3
 4
  • 1

    Your question has some problems: 1) The code is incomplete and with indentation difficult to read 2) You didn’t say clearly what is the error you are having 3) You didn’t make clear what was the expected behavior of the program.

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 to solve your problem. Just replace the for of the main by something that takes the values of the text document and inserts it in the queue via the function Fila* insereFila(Fila* f, int info).

#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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.