Good afternoon, I'm having this error when compiling the program:
Aborted
Aborted
My program copies from a file to a dynamically allocated array using realloc because I do not know the size of rows and columns of the array inside the file and then print it, the file has this format:
....* *.... ..... .*... .....
Follow the code below:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
char* nome = NULL;
char ch;
FILE *arq = NULL;
size_t tam;
int counter = 1, i = 0, j = 0, k = 0, l = 0, aux = 0;
char** campo = NULL;
getline(&nome, &tam, stdin);
nome[strlen(nome) - 1] = '....*
*....
.....
.*...
.....
';
arq = fopen(nome, "r");
if(arq == NULL)
{
printf("Erro, nao foi possivel abrir o arquivo\n");
}
else
{
while( (ch=fgetc(arq))!= EOF)
{
campo = (char**) realloc(campo, (i + 1) * sizeof(char*));
while(ch != '\n')
{
campo[i] = (char*) realloc(campo[i], (i + 1) * sizeof(char));
campo[i][j] = ch;
j++;
aux = j;
}
j = 0;
i++;
}
}
for(k = 0; k < aux; k++)
{
for(l = 0; l < i; l++)
{
printf("%c ", campo[k][l]);
}
printf("\n");
}
fclose(arq);
return 0;
}