Counting Lines in a File: A Homework Statement

In summary: And those are the arguments you passed to fscanf(). Also, in your call to fscanf(), the assignment operator = is incorrect. It should be the equality operator ==.
  • #1
RicardoMarques
20
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
  • #2
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.
 

1. What is the purpose of counting lines in a file?

The purpose of counting lines in a file is to determine the number of lines or rows of data contained within the file. This can be useful for various purposes, such as analyzing data, checking for errors, or organizing information.

2. How do you count lines in a file?

To count lines in a file, you can use a programming language such as Python, Java, or C++, which have built-in functions for reading and counting lines in a file. Alternatively, you can use a command line tool such as wc in Unix or Measure-Object in Windows PowerShell.

3. Can you count lines in a file without using a computer?

Technically, yes, you can count lines in a file without using a computer by manually reading and counting each line. However, this method is time-consuming and prone to human error, so it is not recommended for large files or for accuracy.

4. What is the difference between counting physical lines and logical lines in a file?

Counting physical lines in a file refers to counting the actual lines of text, including blank lines and lines containing only spaces or tabs. On the other hand, counting logical lines in a file involves counting only the lines that contain meaningful information, excluding blank lines and lines with only spaces or tabs.

5. Are there any challenges associated with counting lines in a file?

One of the main challenges with counting lines in a file is handling different file formats and line endings. For example, Windows uses \r\n as the line ending, while Unix uses \n. This can affect the accuracy of line counting if not properly accounted for. Additionally, counting lines in a file with very large or complex data can be time-consuming and resource-intensive.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
837
  • Engineering and Comp Sci Homework Help
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
0
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
25K
  • Programming and Computer Science
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
2
Views
10K
Back
Top