Error reading file

1

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

  • Number of points
  • Coordinates of the points
  • Number of lines
  • Number of vertices of each line
  • Coordinates of each vertex of the line
  • Number 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 the file read, the data is not read correctly. In the main function, you have:

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

Note that in the main function, the variables have been properly 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 you ask 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 is repeated for all other readings, ie it does not continue reading the rest of the data. In addition, the program stops working.

I wonder what might be causing this problem. Remembering that there is no error or warning after compilation.

    
asked by anonymous 01.07.2017 / 02:30

1 answer

1

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

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

Within 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, it starts reading at the beginning of the file. If you pass FILE* , the place where you stopped reading will be used to start the next reading.

    
01.07.2017 / 06:40