Counting Lines in a File: A Homework Statement

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 replies · 1K views
RicardoMarques
Messages
20
Reaction score
2

Homework Statement



Code:
#include <stdio.h>
#include <stdbool.h>

#define MAX_STRING   256
#define MAX_LINHA   1024

typedef struct{
   int dia, mes, ano;
}Data;

int contar_linhas(char fname[]) {
    FILE *f;
   Data
    int soma=0;                        // Inicializa o contador
    f = fopen(fname, "r");             // Tenta abrir o ficheiro para leitura
    if( f == NULL )                    // Testa insucesso
        return -1;
   if(fscanf(f, "%d %d %d", &d.dia, &d.mes, &d.ano)=!3)
        soma++;
    fclose(f);                         // Fecha o ficheiro
    return soma;                       // Retorna o número de linhas contadas
}

int main(void) {
    char fn[MAX_STRING];
    printf("Nome do ficheiro? ");
    scanf("%s", fn);
    int res = contar_linhas(fn);
    if( res == -1 )
        printf("Nao foi possivel abrir o ficheiro\n");
    else
        printf("O ficheiro tem %d linhas\n", res);
    return 0;
}

Homework Equations



There are no relevant equations

The Attempt at a Solution



I tried to change my if parameters... but it keep giving the same error "invalid lvalue of assignment"... that I don't know what means

[/B]
 
on Phys.org
RicardoMarques said:

Homework Statement



Code:
#include <stdio.h>
#include <stdbool.h>

#define MAX_STRING   256
#define MAX_LINHA   1024

typedef struct{
   int dia, mes, ano;
}Data;

int contar_linhas(char fname[]) {
    FILE *f;
   Data     // <--- What is this?
    int soma=0;                        // Inicializa o contador
    f = fopen(fname, "r");             // Tenta abrir o ficheiro para leitura
    if( f == NULL )                    // Testa insucesso
        return -1;
   if(fscanf(f, "%d %d %d", &d.dia, &d.mes, &d.ano)=!3)
        soma++;
    fclose(f);                         // Fecha o ficheiro
    return soma;                       // Retorna o número de linhas contadas
}

int main(void) {
    char fn[MAX_STRING];
    printf("Nome do ficheiro? ");
    scanf("%s", fn);
    int res = contar_linhas(fn);
    if( res == -1 )
        printf("Nao foi possivel abrir o ficheiro\n");
    else
        printf("O ficheiro tem %d linhas\n", res);
    return 0;
}

I tried to change my if parameters... but it keep giving the same error "invalid lvalue of assignment"... that I don't know what means
I see two problems.
1) The second line in contar_linhas() is not a valid declaration statement. Your code won't even compile with this line in it. I added a comment with <-- What is this? in it to identify the line.
2) In your call to fscanf(), the variable d is undeclared and undefined. Apparently your intent is that d be an instance of the Data structure type, but you didn't declare it. Since d is undeclared and undefined, the members d.dia, d.mes, and d.ano are also undefined.