1
I made a program in which reads strings from a file separated by -
(hyphen) and saves each string at a position of a struct vector.
When compiling, 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 file structures. h, however, I would like to leave the implementation hidden, leaving only the prototypes in . h
Would it be possible?
Follows 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);
}
What do you call hiding the implementation? Implementation of what?
– Maniero
I referred to the struct string implementation. I don’t want to implement (define) it in the header file (header) called: structures. h But Dan Getz solved my problem. Plus, thanks for the interest. :)
– Marcos Paulo Rodrigues