File reading

1

I need to read from a file the number of tests to be done and which tests to run. 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 number 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'm having a hard time finding a way to read the rest of the input file. I've 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 does not 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 would like to know a better way to read this file.

    
asked by anonymous 01.07.2017 / 23:46

2 answers

1

One of the problems is that in your code you are not reading until the end of the line. The comments, for example, are not being ignored. In the LeNumeroDeTestes() function you can put a while to advance 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 the following:

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);
}

In addition to what 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.

    
02.07.2017 / 00:39
1

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

See:

#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;
}

Output:

  

4 // Number of tests to be done
  1 LINSIMP 3 // First (1) test that checks if line 3 is simple
  2 LINPOL 3 1 // Second (2) test that checks if line 3 intersects polygon 1
  3 POLSIMP 1 // Third (3) test that checks if polygon 1 is simple
  4 PTOPOL 1 1 // Fourth (4) test that checks if point 1 is in polygon 1

The lerArquivo() function will read and display data for you.

    
02.07.2017 / 00:45