tags:

views:

261

answers:

2

Hi all, 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.

+4  A: 

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;
Cătălin Pitiș
You probably mean "struct Person clovek;" in the second line.
Sii
Ooops, you're right. Too much C++ :)
Cătălin Pitiș
That should be `struct Person clovek;`
Samir Talwar
+11  A: 

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 ];
/* .. */
dirkgently