Error reading string to file

-1

Well I'm reading a file with the formatting of type:

name.txt names columns columns types

Where I can read all these strings, but when printing them, for example in a line with two column names and their respective two types, printf only shows the first two strings and in sequence two nulls . Here is the code:

char **tipos_coluna; 
tipos_coluna = malloc(sizeof(char*)*(2*qtd_colunas)); //Matriz de strings alocada dinamicamente
for (int i = 0; i < qtd_colunas; ++i)
{
    tipos_coluna[i] = malloc(sizeof(char)*20);
}

FILE *colunas_tabelas = fopen("colunas_tabelas/colunas_tabelas.txt", "r");

char validar[50];
while((fscanf(colunas_tabelas, "%s\n", validar)) != EOF)
{
    if( !(strcmp(validar,tabela_url)) )
    {   
        int i=0; 
        while((fscanf(colunas_tabelas, " %s", tipos_coluna[i])) != EOF)
        {

            if(i == (2*qtd_colunas)-1){
                break;
            }

            ++i; 
        }
    }
}

fclose(colunas_tabelas);


for (int i = 0; i < 2*qtd_colunas; ++i)
{
    printf("%s ", tipos_coluna[i]);
}
    printf("\n");
    
asked by anonymous 14.11.2018 / 03:19

1 answer

0
while((fscanf(colunas_tabelas, "%s\n", validar)) != EOF)

There are two errors, in this section there is an inconsistency, since fscanf does not return EOF. I solved taking the line with fgets and formatting it with sscanf.

The second one is that I'm not allocating the right amount of lines

for (int i = 0; i < qtd_colunas; ++i)

What would it be

for (int i = 0; i < 2*qtd_colunas; ++i)
    
15.11.2018 / 16:24