Reputation:
I'm trying to write a program that reads text from external file (string string int, per line). Struct is defined outside of main function:
typedef struct Person {
char fname[15];
char lname[20];
unsigned long int birth;
} clovek;
I don't need "clovek" to be an array as with every line data can be overwritten. Line is red to buffer with:
fgets(buffer, 50, datafile);
Then I want to parse it to the struct but that is where my problem arises:
int i = 0;
while (buffer[i] != ' ') {
clovek.fname[i] = buffer[i];
i++;
}
And this gives me out an error: expected identifier or '(' before '.' token
I also wanted to use this code for debugging but it gives out another error as well:
printf("fname, %s\n", clovek.fname);
error: expected expression before 'clovek'
My guess is that I totaly misunderstood using of struct.
Upvotes: 2
Views: 580
Reputation: 111130
clovek
is an alias for struct Person
. Either remove the typedef
keyword, or create an object of type struct Person
somewhere in your code. With the present code you can do:
clovek someone;
while (buffer[ i ] ) != ' ') {
someone.fname[ i ] = buffer[ i ];
/* .. */
Upvotes: 10
Reputation: 14341
Your statement defines clovek as an alias to the structure Person.
Remove "typedef" from the declaration. This way, clovek becomes a variable of type Person:
Or even better, separate struct declaration from variable declaration:
struct Person {
char fname[15];
char lname[20];
unsigned long int birth;
};
struct Person clovek;
Upvotes: 5