4
I am wanting to read from a file that has an array inside, the amount of rows and columns and printable on the screen, but my program does not appear anything, it simply stops and does not print the number of rows(counterL) and columns(counterC).
The program is just below:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*
A funcao abaixo tem como utilidade a alocacao dinamica de uma matriz
*/
int** alocaMatriz(int linha, int coluna)
{
int i, j;
int** matriz = NULL;
for(i = 0; i < linha; i++)
{
matriz = (int**) malloc(linha * sizeof(int*));
for(j = 0; j < coluna; j++)
{
matriz[i] = (int*) malloc(coluna * sizeof(int));
}
}
return matriz;
}
/*
A funcao abaixo tem como utilidade a liberacao de memoria de uma matriz alocada
dinamicamente
*/
void liberaMatrizes()
{
}
int main (void)
{
int aux, counterL = 0, counterC = 0;
int** matrizRetorno = NULL;
char* nomeArquivo = NULL;
size_t tamanho = 0;
FILE * ponteiro;
getline(&nomeArquivo, &tamanho, stdin);
nomeArquivo[strlen(nomeArquivo) - 1] = '\0';
ponteiro = fopen(nomeArquivo, "r");
while(fscanf(ponteiro, "%d", &aux) != '\n')
{
counterC++;
if(fscanf(ponteiro, "%d", &aux) == '\n')
counterL++;
}
printf("%d\n%d\n", counterL, counterC);
matrizRetorno = alocaMatriz(counterL, counterC);
fclose(ponteiro);
liberaMatrizes();
return 0;
}
But I do not what the length of rows and columns of my file, the person inserts the txt file and my program counts
– Gabriel Vinicius
I don’t know if you read the second link I sent you. Basically if the scanf fails you can leave your pointer in an unknown location bringing future problems. In the case of the size of the rows and columns can always create a string with a size like 4098 or 10240, I doubt that someone write so much in a row and if there should be a troll.
– krystalgamer