Error in reading file

Asked

Viewed 59 times

1

I need to create functions that read from an.txt input file:

  • Number of points
  • Coordinates of the points

  • Number of lines
  • Number of vertices of each line
  • Coordinates of each vertex of the line

  • Quantity of polygons
  • Number of vertices of each polygon
  • Coordinates of each vertex of the polygon

The functions I’ve created are:

int LeNumeroDePontos()
{
    FILE *entrada;
    entrada = fopen("entrada.txt", "r");
    int nponto;
    printf("Digite a quantidade de pontos que se quer (Maximo 100): ");
    fscanf(entrada, "%d", &nponto);
    return nponto;
}

void LePontos(Ponto **ptemp, int npontos)
{
    FILE *entrada;
    entrada = fopen("entrada.txt", "r");
    int i;
    double x, y;
    *ptemp = (Ponto *) malloc(npontos*sizeof(Ponto));
    Ponto *p = *ptemp;
    printf("Digite as coordenadas X e Y:\n");
    for(i = 0; i < npontos; i++)
    {
        fscanf(entrada, "%lf", &x);
        fscanf(entrada, "%lf", &y);
        criaPonto(&p[i], x, y);
    }
    printf("\n");
}

int LeNumeroDeLinhas()
{
    FILE *entrada;
    entrada = fopen("entrada.txt", "r");
    int nlinhas;
    printf("Digite a quantidade de linhas que se quer: ");
    fscanf(entrada, "%d", &nlinhas);
    return nlinhas;
}

int LeNumeroDeVerticesCadaLinha()
{
    FILE *entrada;
    entrada = fopen("entrada.txt", "r");
    int numVertices;
    printf("Digite a quantidade de vertices da linha: ");
    fscanf(entrada, "%d", &numVertices);
    if(numVertices < 2)
        exit(1);
    else
        return numVertices;
}

int LeNumeroDePoligonos()
{
    FILE *entrada;
    entrada = fopen("entrada.txt", "r");
    int npoligonos;
    printf("Digite a quantidade de poligonos que se quer: ");
    fscanf(entrada, "%d", &npoligonos);
    return npoligonos;
}

int LeNumeroDeVerticesCadaPoligono()
{
    FILE *entrada;
    entrada = fopen("entrada.txt", "r");
    int numVertices;
    printf("Digite a quantidade de vertices do poligono: ");
    fscanf(entrada, "%d", &numVertices);
    if(numVertices < 3)
        exit(1);
    else
        return numVertices;
}

When I implemented them, but reading the keyboard data, they all worked correctly. However, when implementing file reading, the data is not read correctly. In the main function, one has:

int main()
{
    FILE *entrada;
    entrada = fopen("entrada.txt", "r");

    int npontos = LeNumeroDePontos();
    LePontos(&Pts, npontos);
    ImprimePontos(Pts, npontos);

    int nlinhas = LeNumeroDeLinhas();
    LeImprimePontosDaLinha(Li, VertLinha, nlinhas);

    int npoligonos = LeNumeroDePoligonos();
    LeImprimePontosDoPoligono(Pol, VertPoligono, npoligonos);

    fclose(entrada);
    return 0;
}

Take into account that in the main function the variables have been duly declared.

The input file is as follows:

5     //Numero de pontos
10 5  //Coordenadas de cada ponto
12 4
13 2
2 1
1 0
2     //Numero de Linhas
3     //Numero de vértices da primeira linha
0 0   //Coordenadas dos vértices da primeira linha
1 1
3 6
4     //Numero de vértices da segunda linha
10 17 //Coordenadas dos vértices da segunda linha
22 38
3 0
7 18
1     //Numero de polígonos
4     //Numero de vértices do primeiro polígono
4 9   //Coordenadas do vértice do primeiro polígono
11 3
2 2
5 10

However, when asking to print the read values on the screen, you have the following result:

(5, 10)
(5, 12)
(4, 13)
(2, 2)
(1, 1)

This is the reading that repeats for all other readings, that is, it does not continue reading the rest of the data. In addition, the program stops working.

I would like to know what may be generating this problem. Remembering that there is no error or warning after compiling.

1 answer

1


The problem is that you are opening the file in each function. Instead of calling

entrada = fopen("entrada.txt", "r");

within the functions, you must pass entrada as

int LeNumeroDePontos(FILE* entrada) {...}
void LePontos(FILE* entrada, Ponto **ptemp, int npontos) {...}
int LeNumeroDeLinhas(FILE* entrada) {...}

Each time you open the file, you start reading at the beginning of the file. If you pass the FILE*, the place where you stopped reading will be used to start the next reading.

Browser other questions tagged

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