Using the getline () function in C with MinGW

0

I've created a code for a bookstore library using Linux / GNU, and it works fine there. However, I need the code compile in Windows using GCC, and I was wondering if there is a method to use this function or if there is a simple way to override it. Here are the snippets of code involved in the questioning:

FILE * arquivo;
Livro * acervo;
int indice = 0, i;    
int ultimo_regnum = 0;

char * linha = NULL;
size_t tamanho = 0;
ssize_t check;

while ((check = getline(&linha, &tamanho, arquivo)) != -1) {
    sscanf(linha, "%[^||]||%[^||]||%[^||]||%[^||]||%[^||]||%hd||%hd||%hd||%d||%f", titulo, editora, autor, genero, encadernacao, &ano, &edicao, &paginas, &regnum, &preco);
    //printf("%s\n%s\n%s\n%s\n%s\n%hd\n%hd\n%hd\n%d\n%f\n", titulo, editora, autor, genero, encadernacao, ano, edicao, paginas, regnum, preco);
    strcpy(acervo[indice].titulo, titulo);
    strcpy(acervo[indice].editora, editora);
    strcpy(acervo[indice].autor, autor);
    strcpy(acervo[indice].genero, genero);
    strcpy(acervo[indice].encadernacao, encadernacao);
    acervo[indice].ano = ano;
    acervo[indice].edicao = edicao;
    acervo[indice].paginas = paginas;
    acervo[indice].regnum = regnum;
    acervo[indice].preco = preco;
    indice++;
    ultimo_regnum = regnum;
    acervo = (Livro *)realloc(acervo, sizeof(Livro) * (indice + 1));
    if (acervo == NULL){
        printf("Erro ao alocar memoria.\n");
        exit(1);
    }
}

fclose(arquivo);

}

I tried to use a #define _GNU_SOURCE but it did not work

    
asked by anonymous 18.11.2018 / 01:28

1 answer

1

It has two possibilities, and in both the idea is to use one of several implementations of the getline function that already exists:

In the first scenario you only create the function if you have to compile in windows environment, testing with #ifdef for the _WIN32 macro:

#ifdef _WIN32

ssize_t getline(char **lineptr, size_t *n, FILE *stream) {
    //resto do código aqui
}

#endif

Alternatively you can simply create your own function with the getline code and give it another name. This will never collide with one that is already set:

ssize_t obterlinha(char **lineptr, size_t *n, FILE *stream) {
    //resto do código aqui
}
    
19.11.2018 / 00:04