Counting Lines in a File: A Homework Statement

Click For Summary
SUMMARY

The forum discussion focuses on a C programming homework assignment involving counting lines in a file using the function contar_linhas. The code contains errors, specifically an invalid declaration of the Data structure instance and a faulty condition in the fscanf function call. The correct implementation requires declaring an instance of Data and using the correct syntax for the fscanf function to avoid compilation errors.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with file handling in C using fopen and fclose
  • Knowledge of the fscanf function for formatted input
  • Basic understanding of data structures in C
NEXT STEPS
  • Learn about proper variable declaration and initialization in C
  • Study the usage of the fscanf function and its return values
  • Explore error handling techniques in file operations in C
  • Review the concept of structures in C and how to instantiate them
USEFUL FOR

C programming students, software developers debugging file handling code, and anyone looking to improve their understanding of structured data in C.

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]
 
Physics news 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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 12 ·
Replies
12
Views
27K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
11K