1
 struct phone
 {
      char fname[20];
      char lname[20];
      char pnumber[20];
 };

int main()
{

 int x=0;
 int n;
 char s[100];

 FILE * f;

 f = fopen("phone book","r");

 if (f != NULL)
 {
     printf("file exist !\n");   
     while(!(feof(f)))
     {
           fscanf(f,"%s,%s,%s",s[x].fname,s[x].lname,s[x].pnumber);
           x++;
     }
 }

 printf("1-   add");
 printf("2-   query");
 scanf("%d",&n);

if(n==1)
    printf("%s",s[n].fname);

if(n==2)
    fclose(f);

}

I write this program and there is a problem in fscanf

the comment for the error

 'main':|
  request for member 'fname' in something not a structure or union|
  request for member 'lname' in something not a structure or union|
  request for member 'pnumber' in something not a structure or union|
  request for member 'fname' in something not a structure or union|
 : variable 's' set but not used [-Wunused-but-set-variable]|

 : control reaches end of non-void function [-Wreturn-type]|
 ||=== Build finished: 4 errors, 2 warnings (0 minutes, 0 seconds) ===|
haccks
  • 104,019
  • 25
  • 176
  • 264
  • Format your code, it's unreadable. ----- "and there is a problem in fscanf" - yeah, `fscanf()` is itself the problem, don't use it. You can use `fgets()` to read lines from a file, then `strchr()`, `strstr()`, `strtok_r()` and `strtol()` to parse each line. –  Dec 23 '13 at 09:59
  • 1
    first, `char s[100];` --> `struct phone s[100];` – BLUEPIXY Dec 23 '13 at 10:03

3 Answers3

1

s is declared to be char s[100];, so s[x] is a char and not a struct phone. You need to declare s as struct phone s[100]; and the first of your problems will go away.

Paul Hankin
  • 54,811
  • 11
  • 92
  • 118
0

For the errors

request for member 'fname' in something not a structure or union|
request for member 'lname' in something not a structure or union|
request for member 'pnumber' in something not a structure or union|
request for member 'fname' in something not a structure or union|
: variable 's' set but not used [-Wunused-but-set-variable]|  

Change

char s[100];  // is not a structure. You need to declare it as structure

to

struct phone s[100];  

and for the error

control reaches end of non-void function [-Wreturn-type]|  

as return type of main is int ( a non-void function whose return type is other than void) you need to add a return 0; statement before the closing brace } of main.

haccks
  • 104,019
  • 25
  • 176
  • 264
0

Well, the compiler is right. You declare

char s[100];

and try to access its members like it would be a structure array. It is just a char array.

I think you wanted to do something like

struct phone s[100];

This will declare an array s of 100 struct phone's.

Also, note that while(!feof(f)) it's not recommended to use in this case: Why is “while ( !feof (file) )” always wrong?

Community
  • 1
  • 1
Paul92
  • 8,827
  • 1
  • 23
  • 37