1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Fprintf error

  1. Dec 4, 2016 #1
    1. The problem statement, all variables and given/known data

    Code (Text):

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

    #define MAX_STRING   256
    #define MAX_LINHA   1024

    typedef struct{
       int dia, mes, ano;

    int contar_linhas(char fname[]) {
        FILE *f;
        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)
        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");
            printf("O ficheiro tem %d linhas\n", res);
        return 0;

    2. Relevant equations

    There are no relevant equations
    3. 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 dont know what means

  2. jcsd
  3. Dec 5, 2016 #2


    Staff: Mentor

    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.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted