Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error: undeclared here (not in a function)

Tags:

c

file-io

struct

I am getting this error on the last line of the code, as I am trying to read data from an extra.txt file. Records are read properly from an input.txt but not sure why it is throwing error for extra file. Thanks!

typedef struct {
    char* fname;
    char* lname;
    int id;
    int age;
} data;

typedef struct {
    data** array;
    int len;
    int cap;
}vector;

vector* vector_read(FILE* in_file)
{
    int i;
    vector *v = (vector*)malloc(sizeof(vector));
    fscanf(in_file,"%d",&v->len);
    if(in_file=NULL)
     {
       return NULL;
     }
    printf("%d",v->len);
    data** array = (data**)malloc(sizeof(data*)*(v->len));
    v->array = array;
    data *temp;

   for(i=0;i<(v->len);i++)
   {    
     temp = data_read(in_file);        
     v->array[i] = temp;
   }

return v;
}

    vector *v = vector_read(input);
    printf( "initial state of vector v\n");
    vector_print(v);
    vector *v_add = vector_read(extra); 

EDIT:

extra.txt has records in this fashion:

4

Barak Obama 101 50  
Joe Biden 102 55  
Joe Plumber 10293 45  
Wayne Gretzky 99 56

and input.txt

1

Aaaa
Aooo
1
20
like image 846
Vbp Avatar asked Oct 15 '25 06:10

Vbp


1 Answers

Your code needs go inside a function.

For example like this:

int read_input_and_extra(FILE * input, FILE * extra)
{
  vector *v = vector_read(input);
  if (!v)
    return -1;

  printf( "initial state of vector v\n");
  vector_print(v);

  vector *v_add = vector_read(extra); 
  if (!v_add)
    return -2;

  return 0;
}
like image 188
alk Avatar answered Oct 16 '25 22:10

alk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!