Fiz um programa em que lê strings de um arquivo separadas por -
(hífen) e salva cada string em uma posição de um vetor de struct.
Ao compilar, gera a seguinte mensagem de erro:
array type has incomplete element type
Pesquisei na net e vi que uma solução seria escrever a implementação da struct dentro do arquivo estruturas.h, porém, eu gostaria de deixar a implementação oculta, deixando apenas os protótipos no .h
Seria possível?
Segue o código:
main.c
#include <stdlib.h>
#include "estruturas.h"
int main()
{
String vetor_de_string[MAX];
leArquivo(vetor_de_string);
return 0;
}
estruturas.h
#ifndef ESTRUTURAS_H_
#define ESTRUTURAS_H_
#define MAX 50
typedef struct string String;
void leArquivo(String *s);
#endif
estruturas.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "estruturas.h"
struct string
{
char nome[20];
};
void leArquivo(struct string *s)
{
FILE *f;
f = fopen("data.txt", "r");
if(!f)
{
printf("*** Erro: Nao foi possivel abrir o arquivo! ***\n");
exit(1);
}
int l = 0, i = 0;
char aux, a[20], b[20], c[20];
while((aux = fgetc(f)) != EOF)
{
if(aux == '\n')
l++;
if(l > 0)
{
fscanf(f, "%19[^-]s", a);
aux = fgetc(f);
fscanf(f, "%19[^-]s", b);
aux = fgetc(f);
fscanf(f, "%19[^\n]s", c);
strcpy(s[i].nome, a);
strcpy(s[++i].nome, b);
strcpy(s[++i].nome, c);
i++;
}
}
fclose(f);
}