C Language - Read File to Save to Lists

Asked

Viewed 131 times

0

Imagine a school with several classrooms, and each room has several students

I’m having trouble reading file with the name of the room, number of places by rows (rows and columns) I have the following structure!

typedef struct
{
    char nome[50];
    int numero;
}Aluno;

typedef struct nodo{
    Aluno dados;
    struct nodo *anterior;
    struct nodo *seguinte;
}Nodo;

typedef struct{
    char sala[50];
    int linhas, colunas;
    Nodo inicio;
} Lista;

And the next to read from the file, but I know it’s wrong

int LerFicheiro(char *nf)
{
    FILE* w=fopen(nf, "r");
    Lista* li;

    int l,c;
    while (!feof(w))
    {
        fread(&li->nome, sizeof(Lista), 1, w);  // nome da sala
        fread(&li->linhas, sizeof(Lista), 1, w); //  nº linhas
        fread(&li->colunas, sizeof(Lista), 1, w);  // nº colunas

        for (l = 0; l <= li->linhas; l++)
        {
            for (c = 0; c < li->colunas; c++)
            {
                /*LER NOMES E NUMEROS*/


            }
        }
    }
    fclose(w);

}

This is the file to read! It has the name of the room, in the following row the number of places in line and in column!!! Next is the student’s number and the name of the respective student

Sala de Historia
8 5
1231 
Joao Afonso
563
Zé Alves
7634
Rita Pereira
948
Patricia Mendes
39
Rui Santos

Someone can make me or help me in this?

Thank you very much!!!!

1 answer

0

fscanf(f, "%d", &Imag->NLINHAS);

Usually I use "fscanf" instead of "fread" to read the contents of the file.

    fscanf(f,"%s" ,&li->titulo);  // nome da imagem
    fscanf(f,"%d",&li->linhas); //  nº linhas
    fscanf(f,"%d",&li->colunas);  // nº colunas
    fscanf(f,"%d",&li->canais);  // nº canais

The structures you put up are relative to the room, but the code for reading the file is relative to an image, right?? Anyway, you still have to define in the structure List the title and the channels, and you want those variables in the read file.

typedef struct{
char sala[50];
char titulo[50];
int linhas, colunas,canais;
Nodo inicio;
} Lista;
  • Yes, my mistake, I’ve edited!!

  • then you can still use what I sent you before, but replace the variables you had with the new ones (name, rows, columns)

  • But, how will I do with the classrooms? my doubt is even this, read room by room the places with the students

  • How have you been writing in the file? can you print out how the file is? so it’s easier to figure out how to help you

Browser other questions tagged

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