0

I have a header file named store.h which contains a struct and my main is located in E.c,i am currently experiencing an error which i can not comprehend why it is happening. Any help is much appreciated

store.h:

struct st{
    char *buf,*var;
    int line_number;
    char *keywords;
    char *operators;
};

E.C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "store.h"
#define max 1000000
int main(int argc,char *argv[]){

    FILE *fp;
    int ag=0;
    
    fp=fopen( argv[1],"r");
    if(fp==NULL){
        printf("error cannot open file\n");
        exit(1);
    }                                  

    while(feof(fp)==0){  
        *buf=fgets(*buf,max,fp);
    
    // irrelevant commands follow
    
    }

    fclose(fp);
}

On the line *buf=fgets(*buf,max,fp); I get the error:‘buf’ undeclared (first use in this function), however I have declared it in my struct and I have included the file that contains the struct, where am I wrong?

Christian Gibbons
  • 4,272
  • 1
  • 16
  • 29
David Brawl
  • 3
  • 1
  • 7
  • 2
    `buf` is a member of structure. So it needs to be accessed as a member of a structure. – Eugene Sh. Mar 03 '21 at 17:34
  • oh i see, thanks for the explanation, much appreciated – David Brawl Mar 03 '21 at 17:36
  • I'd say the error you get is because you need to go back to your beginners book, tutorial or class and read more. Not only about structures and how to handle them, but also on basic I/O like how the [`fgets`](https://en.cppreference.com/w/c/io/fgets) function really works. – Some programmer dude Mar 03 '21 at 17:36
  • Ensure that you allocate a sufficient amount of memory before taking the pointers in use. Otherwise, you'll get a hit from segfault. – Rohan Bari Mar 03 '21 at 17:37
  • yeah i will most surely have another look on structs and stdio funtions, as i am a beginner. – David Brawl Mar 03 '21 at 17:37
  • 1
    please read [why is while(!feof(f) always wrong](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) – user3629249 Mar 03 '21 at 19:04
  • OT: regarding: `printf("error cannot open file\n");` error messages should be output to `stderr`,not `stdout` When the error is from a C library function, should also output to `stderr` the reason the system thinks the error occurred. Suggest: `perror( "fopen failed" );` as that function is made for this purpose – user3629249 Mar 03 '21 at 19:09

1 Answers1

2
  1. buf is a member of the struct.
  2. You need to define the struct object.
  3. You need to allocate memory for buf.
  4. You should not assign buf with return value as it may cause a memory leak
  5. Why is “while ( !feof (file) )” always wrong?
int main(int argc,char *argv[]){
    FILE *fp;
    struct st s;
    int ag=0;

    s.buf = malloc(max);
    if(!s.buf) exit(2);
    fp=fopen( argv[1],"r");
    if(fp==NULL){
        printf("error cannot open file\n");
            exit(1);
    }
                                            

    while(fgets(s.buf,max,fp)){  
         /* ...*/ 
    }
     /* .... */
0___________
  • 60,014
  • 4
  • 34
  • 74