File reading

Asked

Viewed 363 times

1

I need to read from a file the amount of tests to be done and which tests to be done. The input file is as follows:

4            //Numero de testes a serem feitos
1 LINSIMP 3  //Primeiro(1) teste que verifica se a linha 3 é simples
2 LINPOL 3 1 //Segundo(2) teste que verifica se a linha 3 intercepta poligono 1
3 POLSIMP 1  //Terceiro(3) teste que verifica se o poligono 1 é simples
4 PTOPOL 1 1 //Quarto(4) teste que verifica se o ponto 1 está no poligono 1

The amount of tests (which are four) has already been implemented through the function:

int LeNumeroDeTestes(FILE *entrada)
{
    int numTestes;
    fscanf(entrada, "%d", &numTestes);
    return numTestes;
}

However, I am having trouble finding a way to read the rest of the input file. I have implemented the following:

int i, LinhaTeste;
char Teste[10];
for(i = 0; i < numTestes; i++)
{
    fscanf(entrada, "%d", &i);
    fscanf(entrada, "%s", Teste);
    if(i == 0)
    {
        fscanf(entrada, "%d", &LinhaTeste);
        printf("%d %s %d", i, Teste, LinhaTeste);
    }
}

But it doesn’t look like a very good solution. Also, although no compilation errors occur, nothing is printed on the screen. (I did not finish the cases of i = 1, 2 and 3).

I wish I knew a better way to read this file.

  • Vc only qr display all its content on the screen or want to display a specific content of it?

  • I wanted to display on the screen just to know if I am reading correctly, because actually, it is not necessary to print. What will be printed in the exercise is another content.

  • I’m not understanding q vc qr. Could be more specific regarding your difficulty?

  • What I really need is to just read that input file that was specified above. However, I don’t know if I am reading it correctly. The printing part is just to see if I am reading the data correctly. My main question is: How to read input data correctly.

2 answers

1


One of the problems is that in your code you are not reading to the end of the line. Comments, for example, are not being ignored. In function LeNumeroDeTestes() you can put a while to proceed to the end of the line, as follows:

int LeNumeroDeTestes(FILE *entrada)
{
    int numTestes;
    fscanf(entrada, "%d", &numTestes);
    while (fgetc(entrada) != '\n');
    return numTestes;
}

To read the rest of the file, you can do as follows:

int i, LinhaTeste;
char Teste[10];
for(i = 0; i < numTestes; i++)
{
    fscanf(entrada, "%d %s", &LinhaTeste, Teste); # Lê o número e o nome do teste (1ª e 2ª colunas)
    while (fgetc(entrada) != '\n'); # Avança até o fim da linha, ignorando o restante das informações
    printf("%d %s %d\n", i, Teste, LinhaTeste);
}

Also that has been done, you will probably need to interpret the parameters that come after the test name. So instead of simply advancing to the end of the line you will need a loop that reads each parameter until you find: or the end of the line; or the comment mark, from which you advance to the end of the line.

1

One way to read the file is to get all its characters in a loop.

Behold:

#include <stdio.h>

void lerArquivo(FILE * arq)
{
    int c;

    while ((c = getc(arq)) != EOF)
        putchar(c);
}

int main(void)
{
    FILE *arq = NULL;
    arq = fopen("arq.txt", "r");

    if (arq)
    {
        lerArquivo(arq);
        fclose(arq);
    }

    return 0;
}

Exit:

4 //Number of tests to be performed
1 LINSIMP 3 //First(1) test whether line 3 is simple
2 LINPOL 3 1 //Second(2) test whether line 3 intercepts Poligono 1
3 POLSIMP 1 //Third(3) test whether Poligono 1 is simple
4 PTOPOL 1 1 //Room(4) test whether point 1 is in Poligono 1

The function lerArquivo() reads and displays the data for you.

Browser other questions tagged

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