I made a program that read strings of a file separated by -
(hyphen) and saves each string in a position of a struct vector.
When compiling, it generates the following error message:
array type has incomplete element type
I searched the net and saw that a solution would be to write the struct implementation inside the structures.h file, however, I'd like to leave the implementation hidden, leaving only the prototypes in .h
Would it be possible?
Follow the code:
main.c
#include <stdlib.h>
#include "estruturas.h"
int main()
{
String vetor_de_string[MAX];
leArquivo(vetor_de_string);
return 0;
}
structures.h
#ifndef ESTRUTURAS_H_
#define ESTRUTURAS_H_
#define MAX 50
typedef struct string String;
void leArquivo(String *s);
#endif
structures.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);
}